首页 > 这个页面的多页列表该如何抓取?

这个页面的多页列表该如何抓取?

页面地址:
http://www.cpcb.com.cn/Front.aspx/ZbggNewAndOld/1?typeID=%E7%9B%91%E7%90%86

我使用的是 Python3 的 requests 搭配 bs4 和 正则表达式抓取页面的。

url中?前的那个数字是页标号,总页数也能从页面的脚本代码中用正则获取。

遇到的难点是在使用这个url请求时,经常会返回 “没有您需要的信息” 的页面。

我感觉可能是 cookie 的问题,所以我用如下代码进行了测试:

import requests

# 模拟 get 请求的 headers
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
           'Accept-Encoding': 'gzip, deflate, sdch',
           'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
           'Cache-Control': 'max-age=0',
           'Connection': 'keep-alive',
           'Host': 'www.cpcb.com.cn',
           'Referer': 'http://www.cpcb.com.cn/Front.aspx/ZbggNewAndOld/',
           'Upgrade-Insecure-Requests': '1',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
                         '(KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'}

# 从网站首页获取 cookie
host = 'http://www.cpcb.com.cn/front.aspx'
r = requests.get(host, headers=headers)
cookie = r.cookies

# 获取页面
url = 'http://www.cpcb.com.cn/Front.aspx/ZbggNewAndOld/1?typeID=%E7%9B%91%E7%90%86'
r = requests.get(url, headers=headers, cookies=cookie)
page = r.text
print(page)

测试结果是依然会有一定概率出现 “没有您需要的信息”。
如果直接在浏览器输入该地址,也会有出现该情况的可能。

请问该如何改进,才能提高抓取的成功率?

【热门文章】
【热门文章】