在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 獲取列表時,如何翻頁?鏈接都獲取到了,怎么取出、依次訪問?

獲取列表時,如何翻頁?鏈接都獲取到了,怎么取出、依次訪問?

#-*- coding:utf-8 -*-

import requests
import re

# 獲取列表頁鏈接
for page in range(1, 67):
    url_list = 'http://top.chinaz.com/hangye/index_news_{}.html'.format(page)

content_list = requests.get(url_list)
content_list.encoding = 'utf-8'
content_list = content_list.text
contents_list = re.search('''<h3 class="rightTxtHead"><a href="(.*?)" title='.*?</a>''',content_list) # 獲取內(nèi)頁鏈接

source_list = 'http://top.chinaz.com' + contents_list.group(1) # 拼接內(nèi)頁地址
source_contents = requests.get(source_list)
source_contents.encoding = 'utf-8'
source_contents = source_contents.text

website_name = re.search('<h2.*?>(.*?)</h2><p class="plink ml5 fl"><a href="(.*?)" target="_blank" >.*?</a></p>',source_contents,re.S)

print(source_list)
print(website_name.group(1),website_name.group(2))

有點偽代碼... url_list 獲取了所有的列表頁,接著怎么取出第一個列表頁、取出第一個列表頁中的第一個詳情頁、獲取內(nèi)容呢?然后循環(huán)取出第一個列表頁的第二個詳情頁獲取內(nèi)容?

回答
編輯回答
扯不斷

可以考慮用 BeautifulSoup:

import requests
from bs4 import BeautifulSoup

url = 'http://top.chinaz.com/hangye/index_news_3.html'.format(page)
res = requests.get(url)
res.encoding = 'utf-8'
content = res.text
soup = BeautifulSoup(content, 'html5lib')
lst = soup.find_all('h3', class_='rightTxtHead')
for h3 in lst:
    print(h3.a['href'], h3.a['title'])

結(jié)果:

/Html/site_qlwb.com.cn.html 齊魯晚報網(wǎng)
/Html/site_ynet.com.html 北青網(wǎng)
/site_news.cnhubei.com.html 荊楚網(wǎng)新聞頻道
/Html/site_hunantv.com.html 芒果TV
/Html/site_henan100.com.html 河南一百度
/Html/site_pep.com.cn.html 人民教育出版社
/Html/site_cnbeta.com.html cnBeta.COM_中文業(yè)界資訊站
/Html/site_wenming.cn.html 中國文明網(wǎng)
/Html/site_ettoday.net.html ETtoday 東森新聞云
/Html/site_zjstv.com.html 浙江衛(wèi)視官方網(wǎng)站
/Html/site_chengdu.cn.html 成都全搜索
...

我回答過的問題: Python-QA

2018年8月6日 09:04