#-*- 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) # 获取内页链接
source_list = 'http://top.chinaz.com' + contents_list.group(1) # 拼接内页地址
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
获取了所有的列表页,接着怎么取出第一个列表页、取出第一个列表页中的第一个详情页、获取内容呢?然后循环取出第一个列表页的第二个详情页获取内容?
可以考慮用 BeautifulSoup:
結果:
我回答過的問題: Python-QA