python 很简单的爬虫问题

[复制链接]
查看11 | 回复4 | 2021-1-27 06:16:35 | 显示全部楼层 |阅读模式
'''
parse模拟post
分析百度翻译
1:打开网页源代码
2:输入girl,输入一个letter有一个request
3:请求地址:http://fanyi.baidu.com/sug
4:发现formdatakw:girl
5:return格式是json==>needpackagejson
'''

fromurllibimportparse,request
#managejsonmoudel
importjson
'''
1:data构造,然后urlopen打开
2:返回jsonstyleresult
3:encode'girl'
'''
baseurl='http://fanyi.baidu.com/sug'
#存放form==>dictstyle

data={
'ke':'girl'
}

#enode
data=parse.urlencode(data).encode('utf-8')#type-bytes
##encodestringin'utf-8'style==changestyletobytes,notchangecontent

#requestheaded(includedata_lenth)
headers={
#postneedcontent-lenth
'Content-Lengh':len(data)
}
#wehaverequestheader,tryrequest
req=request.Request(url=baseurl,data=data,headers=headers)
rsp=request.urlopen(req)
json_data=rsp.read().decode('utf-8')
print(type(json_data))#str
#changestylestrtodict
json_data=json.loads(json_data)
print(type(json_data))#dict
print(json_data)


分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:16:35 | 显示全部楼层


运行完是这个

回复

使用道具 举报

千问 | 2021-1-27 06:16:35 | 显示全部楼层


问题在哪里,一头雾水
回复

使用道具 举报

千问 | 2021-1-27 06:16:35 | 显示全部楼层
引用1楼qq_39871498的回复:

运行完是这个

你的意思是,你调试下,有没有进入到翻译页面,然后发送请求,推荐用fiddle抓包看看。


回复

使用道具 举报

千问 | 2021-1-27 06:16:35 | 显示全部楼层
这个应该是post请求,代码也有问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行