|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def urlopen2(url,Cookie):
- head = {}
- head['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
- head['Accept-Language']='zh-CN,zh;q=0.9'
- head['Cache-Control']='no-cache'
- head['Connection']=' keep-alive'
- # Cookie 这个要改不同
- #head['Cookie']= Cookie
-
- head['Pragma']='no-cache'
-
- head['Referer']='http://ce.esnai.net/c/acchunan/'
-
- head['Upgrade-Insecure-Requests']='1'
-
- head['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
- req = urllib.request.Request(url,headers = head)
- #加入headers
-
- html = urllib.request.urlopen(req)
- #访问时加入了headers Cookie
- html = html.read()
- return html
复制代码
上面的有加入head , 如果要加入一个代理ip是加到那里
、
比如ip为 222.73.130.111:8080
- import urllib.request
- proxy = urllib.request.ProxyHandler({"http":"222.73.130.111:8080"})
- opener = urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
- urllib.request.install_opener(opener)
- data = urllib.request.urlopen(url).read().decode("utf-8")
复制代码
试一下,很长时间时间没用过urllib了。
|
|