这是我第一次使用session.post我模拟移动浏览器来抓取一个网站。
代码:
import requests
from bs4 import BeautifulSoup
import json
session=requests.Session()
headers={
'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36'}
data={
"flag":1,"id":"419302","htype":1,"sort":{"idx":4,"size":10,"sort":2,"ord":1},
"search":{"kword":"","gtype":4,"opr":0,"ctrl":14,"filters":[]},"alliance":{"aid":"4897","sid":"130026","ouid":"","ishybrid":0},
"Key":"b2d4a14cd19fa0e656d35f065bdcdd16",
"head":{"cid":"09031140210396054230","ctok":"","cver":"1.0","lang":"01","sid":"8888","syscode":"09","auth":None,"extension":
[{"name":"pageid","value":"228032"},{"name":"webp","value":1},{"name":"referrer","value":"http://www.ctrip.com/"},{"name":"protocal","value":"http"}]},
"contentType":"json"}
url='http://m.ctrip.com/restapi/soa2/10soup.text935/hotel/booking/commentgroupsearch?_fxpcqlniredt=09031140210396054230'
soup=session.post(url,headers=headers,files=data)
print soup
错误:
'dict' object has no attribute 'read'
答案 0 :(得分:2)
您想将data
dict转换为字符串,然后使用data=
的{{1}}参数发送:
session.post()
或者,如果您使用的是soup = session.post(url, headers=headers, data=json.dumps(data))
版本2.4.2或更新版本,则可以使用requests
参数直接传递您的dict:
json=
使用soup = session.post(url, headers=headers, json=data)
检查您的请求版本。