python爬取大众点评_网页爬虫 - python爬取大众点评网伪装浏览器返回出现403错误...

问 题

hi,各位好,我在爬大众点评的这个网页http://www.dianping.com/search/keyword/2/75_%E6%96%B0%E4%B8%9C%E6%96%B9,用的request包获取网页源码,header主要添加了user-agent和cookie,但为什么总是返回错误?求助,谢谢各位了!

PS:用curl可以返回正常的源码,没有被封IP

import requests

def gethtml(url, headers):

html = requests.get(url, headers = headers)

html.encoding = 'utf-8'

return html.text

head = {'User_Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2723.3 Safari/537.36',

'Cookie': 'PHOENIX_ID=0a650c81-154a0633f47-a97843; _hc.v="\"e27e18eb-3a3d-4b40-b06a-cbe624c96048.1462979739\""; s_ViewType=10; JSESSIONID=877B00919AD417544F72F5A9953E54B4; aburl=1; cy=2; cye=beijing'}

url = 'http://www.dianping.com/search/keyword/2/75_%E6%96%B0%E4%B8%9C%E6%96%B9'

html = gethtml(url, head)

print(html)

解决方案

已经解决,header中添加

{'Accept': 'application/json, text/javascript'}就能正常返回了

wx.jpg

扫一扫关注IT屋

微信公众号搜索 “ IT屋 ” ,选择关注与百万开发者在一起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值