鱼C论坛

 找回密码
 立即注册
查看: 2089|回复: 2

[已解决]urllib.request 设置代理ip问题

[复制链接]
发表于 2019-3-21 22:53:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. def urlopen2(url,Cookie):



  2.     head = {}
  3.     head['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'

  4.     head['Accept-Language']='zh-CN,zh;q=0.9'

  5.     head['Cache-Control']='no-cache'

  6.     head['Connection']=' keep-alive'

  7.     # Cookie 这个要改不同

  8.     #head['Cookie']= Cookie
  9.    
  10.     head['Pragma']='no-cache'
  11.    
  12.     head['Referer']='http://ce.esnai.net/c/acchunan/'
  13.    
  14.     head['Upgrade-Insecure-Requests']='1'
  15.    
  16.     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'

  17.     req = urllib.request.Request(url,headers = head)
  18.     #加入headers
  19.    
  20.     html = urllib.request.urlopen(req)
  21.     #访问时加入了headers Cookie

  22.     html = html.read()

  23.     return html
复制代码


上面的有加入head ,   如果要加入一个代理ip是加到那里

比如ip为 222.73.130.111:8080
最佳答案
2019-3-22 11:49:57

  1. import urllib.request
  2. proxy = urllib.request.ProxyHandler({"http":"222.73.130.111:8080"})
  3. opener = urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
  4. urllib.request.install_opener(opener)
  5. data = urllib.request.urlopen(url).read().decode("utf-8")
复制代码


试一下,很长时间时间没用过urllib了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-22 11:49:57 | 显示全部楼层    本楼为最佳答案   

  1. import urllib.request
  2. proxy = urllib.request.ProxyHandler({"http":"222.73.130.111:8080"})
  3. opener = urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
  4. urllib.request.install_opener(opener)
  5. data = urllib.request.urlopen(url).read().decode("utf-8")
复制代码


试一下,很长时间时间没用过urllib了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-22 22:25:34 | 显示全部楼层
付小聪 发表于 2019-3-22 11:49
试一下,很长时间时间没用过urllib了。

谢谢  是可以
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-21 03:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表