爬虫-自动模拟http请求

自动模拟http请求:
客户端如何要与服务器端进行通信,需要通过http请求进行,http请求
有很多种
post请求方式:
get请求方式:
示例代码:
import urllib.request
keywd = 'python'
url = 'http://baidu.com/s?wd='+keywd
req = url.request.Request(url)
data = urllib.request.urlopen(req).read()
fh = open('D://1.html','wb')
fh.write(data)
fh.close()
 
 
解决编码的中文报错问题(urllib,request.quote()方法):
import urllib.request
keywd = '你好'
key = urllib.request.quote(keywd)
url = 'http://baidu.com/s?wd='+keywd
req = url.request.Request(url)
data = urllib.request.urlopen(req).read()
fh = open('D://1.html','wb')
fh.write(data)
fh.close()
 
请求中要加入headers等信息,要用到urllib.request.Request
原文地址:https://www.cnblogs.com/zxzx1/p/10902987.html