为什么requests.get无法访问网页?

jhc753951 2018-04-30 07:48:16
import json
import re
from urllib.parse import urlencode
import requests
from bs4 import BeautifulSoup
from requests import RequestException


def get_one_index(offset, keyword):
data = {
'offset': offset,
'format': 'json',
'keyword': keyword,
'autoload': 'true',
'count': '20',
'cur_tab': 1
}
url = 'https://www.toutiao.com/search_content/?' + urlencode(data)
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
return None
except RequestException:
print('请求索引页出错')
return None


def parse_page_index(html):
data = json.loads(html) # json.load用法:原本是字符串形式,转化成一个对象
if data and 'data' in data.keys(): # keys用法:返回字典的所有键值
for item in data.get('data'):
yield item.get('article_url')


def get_page_detail(url):
try:
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.text
return None
except RequestException:
print(u'请求详情页出错')
return None


def parse_page_detail(html): # 解析详情页
soup = BeautifulSoup(html, 'lxml')
title = soup.select('title')[0].get_text()
print(title)
images_pattern = re.compile('var gallery = (.*?);', re.S)
result = re.search(images_pattern, html)
if result:
print(result.group(1))


def main():
html = get_one_index(0, '街拍')
for url in parse_page_index(html):
html = get_page_detail(url)
if html:
parse_page_index(html)


main()


想要制作一个爬取今日头条街拍的网络爬虫,但是最后结果一直显示详情页出错,加了headers也没用,求大神指教!
...全文
1618 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2018-05-01
  • 打赏
  • 举报
回复
看看http返回的错误码是什么。估计反爬虫比较厉害
toznm00 2018-05-01
  • 打赏
  • 举报
回复
用selenium吧,用你的代码post完人家返回的信息里面没有对应的内容的,被挡掉了
jhc753951 2018-05-01
  • 打赏
  • 举报
回复
引用 1 楼 oyljerry 的回复:
看看http返回的错误码是什么。估计反爬虫比较厉害
网页代码200,没错啊- -很奇怪

37,744

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧