zoukankan      html  css  js  c++  java
  • Python urllib2 proxy

    在 正式并入某大公司之后,网络必须设置为统一的proxy,好的方面没看到,但是立即让我一的一个小工具不能工作了。
    在之前使用urllib2库,无需设置proxy,一切工作正常。在必须使用proxy之后,遇到了一系列的问题

    1. 使用urllib2的proxy

    import urllib2
    
    enable_proxy = True
    proxy_handler = urllib2.ProxyHandler({"http" : 'your_proxy'})
    null_proxy_handler = urllib2.ProxyHandler({})
    
    if enable_proxy:
        opener = urllib2.build_opener(proxy_handler)
    else:
        opener = urllib2.build_opener(null_proxy_handler)
    
    urllib2.install_opener(opener)
    

     
    结果得到错误如下:
    URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
    2. 使用requests

    import requests
    r = requests.get("http://www.google.com", proxies={"http": "your_proxy"})
    print r.text
    

     结果:
    可以访问,但是内容不对

    3. 使用requests的proxy

    r = requests.get('http://www.thepage.com', proxies={"http":"your_proxy"})
    print r.content
    

     4. 使用https_proxy

    import requests
    
    proxies = {
      "http": "your_http_proxy",
      "https": "your_https_proxy",
    }
    
    r = requests.get("http://www.google.com", proxies=proxies)
    print r.content
    

     结果:
    可以访问,内容不对
    4. 使用selenium

    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get(url)
    html_resource = driver.page_source
    

     结果:可以访问,内容正确

  • 相关阅读:
    JS高级程序设计 第三章笔记
    JS高级程序设计第二章
    JS高级程序设计 第一章读书笔记
    markdown 尝试
    实验九
    第六章总结
    实验五
    第五章总结
    实验四
    实验三
  • 原文地址:https://www.cnblogs.com/bluescorpio/p/3690837.html
Copyright © 2011-2022 走看看