python爬虫3之http get请求模拟
来源:互联网 发布:考试经文知乎 编辑:程序博客网 时间:2024/06/03 20:27
如果要进行客户端和服务端的消息传递,我们可以使用Http协议请求进行。
GET请求会通过URL网址传递信息比如在百度上查找一个关键字hello,使用爬虫自动实现这个过程。
思路如下:
(1)构建对应的url地址,该URL地址包含GET请求的字段名和字段内容等信息,并且URl满足get请求的格式,即“http://网址? 字段名1=字段内容&字段名2=字段内容2“
(2)以对应的URL为参数,构建Request对象。
(3)通过urlopen()打开构建的request对象。
(4)按需求进行之后的操作。
import urllib.requestkeywd="hello"url="http://www.baidu.com/?wd="+keywdreq=urllib.request.Request(url)data=urllib.request.urlopen(req).read但是当检索的内容是中文时,会出现编码错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 9-10: ordinal not in range(128)此时可以对代码进行修改:
import urllib.requestkeywd="天才"key_code=urllib.request.quote(keywd)url="http://www.baidu.com/?wd="+key_codereq=urllib.request.Request(url)data=urllib.request.urlopen(req).read
阅读全文
0 0
- python爬虫3之http get请求模拟
- python爬虫之GET请求
- python爬虫(二)之get请求
- python socket模拟一个http Get请求页面
- Android 模拟 Http GET 请求
- 模拟http的get请求
- python 模拟Http请求
- python get post模拟请求
- PHP之文件操作,http请求数据格式,模拟get和post,CURL模拟请求的使用
- python发送HTTP GET请求
- python发送HTTP GET请求
- Python发送HTTP GET请求
- Python - Http Get Post请求
- Python实现HTTP GET请求
- Java利用Http 模拟POST、GET请求
- java模拟HTTP post,get请求
- Java模拟Http post、get请求
- 模拟打开浏览器发送http get请求
- SAP HANA Odata的POST/GET/PUT等成功标志201
- IO流之对象流
- javaBean的基本概念及使用
- ArcMap多边形边界平滑Smooth
- Json
- python爬虫3之http get请求模拟
- 1440 [AA]重生之我是Zarc_______厉兵秣马
- 图像特征提取之LBP特征
- 快速WinForm开发
- Android中的属性,是怎么应用到控件上的(一)
- Java猴子选王问题(约瑟夫环)
- EL表达式详解
- scala的闭包
- C 字符串的输入输出