鱼C论坛

 找回密码
 立即注册
查看: 3240|回复: 4

[技术交流] [爬虫] urllib使用ProxyHandler时可缺省参数不使用代理

[复制链接]
发表于 2021-4-27 20:11:07 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 591821661 于 2021-10-5 08:20 编辑

同样的代码

urllib被远程拒绝[10014] (经常抽风,有时又可以,感兴趣的可以试试)

requests一直能用


  1. import urllib.request
  2. import json
  3. import sys
  4. import requests

  5. def useproxy(proxy='127.0.0.1:80'):
  6.     proxy_support = urllib.request.ProxyHandler({'http': proxy})
  7.     opener = urllib.request.build_opener(proxy_support)
  8.     opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'),
  9.                          ('Host','ip-api.com'),
  10.                          ('Connection','keep-alive'),
  11.                          ('Upgrade-Insecure-Requests',1),
  12.                          ('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'),
  13.                          ('Accept-Encoding','gzip, deflate'),
  14.                          ('Accept-Language','zh-CN,zh;q=0.9'),
  15.                          ]
  16.     urllib.request.install_opener(opener)
  17.         
  18. def get_ip():
  19.     # Get IP Address
  20.     try:
  21.         ip_api_url = 'http://ip-api.com/json/?fields=61439'
  22.         ip_result = urllib.request.urlopen(ip_api_url)
  23.         proxy_text = ip_result.read().decode('utf-8')
  24.         s_get_dict = json.loads(proxy_text)
  25.         print('----------------------IP Information Begin--------------------------')
  26.         for key in s_get_dict:
  27.             print('%s : %s' % (key, s_get_dict[key]))
  28.         print('----------------------IP Information End--------------------------')
  29.     except:
  30.         print('url_get error')
  31.         print(sys.exc_info()[0], sys.exc_info()[1])


  32. ip_api_url = 'http://ip-api.com/json/?fields=61439'   
  33. #useproxy()
  34. get_ip()
  35. #ip_result = urllib.request.urlopen(ip_api_url)
  36. #s = requests.get(ip_api_url)
复制代码

问题已经解决:

  1. proxy_handler = urllib.request.ProxyHandler({})  #正确写法
  2. proxy_handler = urllib.request.ProxyHandler(
  3.    {
  4.     'http':'127.0.0.1:80',
  5.     'https':'127.0.0.1:80',
  6.    }
复制代码


)

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
yayc_zcyd + 2 + 2 无条件支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-27 20:36:19 From FishC Mobile | 显示全部楼层
哈哈哈啊哈哈哈哈哈哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-27 20:41:44 | 显示全部楼层
我感觉楼主在开车,但是我没有证据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-27 20:45:18 | 显示全部楼层


标题可以,哈哈

urllib:小瓶张嘎

requests:举起酒来

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-4-27 21:03:03 | 显示全部楼层
本帖最后由 591821661 于 2021-4-27 21:14 编辑

我不是水贴啊,分享干货
找到为啥时好时坏的原因了!!
一句话总结:

不要在ProxyHandler中使用127.0.0.1:80

因为之前有项目用了127.0.0.1:80 能正常运行 就没往这个方向考虑!
看来这个网站比较皮哦

proxy_handler = urllib.request.ProxyHandler({})  正确写法
proxy_handler = urllib.request.ProxyHandler(
   {
    'http':'127.0.0.1:80',
    'https':'127.0.0.1:80',
   }
)
错误示范

2021-04-27_21-04-37 by Sharpstar_kylin.png

测试代码
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Apr 27 19:32:49 2021

  4. @author: 591821661
  5. """
  6. import urllib.request
  7. import json
  8. import sys

  9. def useproxy(proxy=''):
  10.     if proxy:
  11.         proxy_support = urllib.request.ProxyHandler({
  12.                 'http'  : proxy,
  13.                 'https' : proxy,
  14.                 })
  15.     else:
  16.         proxy_support = urllib.request.ProxyHandler({
  17.                 })
  18.    
  19.     opener = urllib.request.build_opener(proxy_support)
  20.     opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'),
  21. #                         ('Host','ip-api.com'),
  22. #                         ('Connection','keep-alive'),
  23. #                         ('Upgrade-Insecure-Requests',1),
  24. #                         ('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'),
  25. #                         ('Accept-Encoding','gzip, deflate'),
  26. #                         ('Accept-Language','zh-CN,zh;q=0.9'),
  27.                          ]
  28.     urllib.request.install_opener(opener)
  29.         
  30. def get_ip():
  31.     # Get IP Address
  32.     try:
  33.         ip_api_url = 'http://ip-api.com/json/?fields=61439'
  34.         ip_result = urllib.request.urlopen(ip_api_url)
  35.         proxy_text = ip_result.read().decode('utf-8')
  36.         s_get_dict = json.loads(proxy_text)
  37.         print('----------------------IP Information Begin--------------------------')
  38.         for key in s_get_dict:
  39.             print('%s : %s' % (key, s_get_dict[key]))
  40.         print('----------------------IP Information End--------------------------')
  41.     except:
  42.         print('url_get error')
  43.         print(sys.exc_info()[0], sys.exc_info()[1])

  44. if __name__ == '__main__':  # 如果运行该文件,而不是作为库函数导入 则运行下列代码
  45.     ip_api_url = 'http://ip-api.com/json/?fields=61439'   
  46.     useproxy()  # 正常
  47.     #useproxy('127.0.0.1:80')  # 会报错
  48.     get_ip()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 12:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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