Home > Database > Mysql Tutorial > body text

Urllib2 Proxy

WBOY
Release: 2016-06-07 16:27:58
Original
849 people have browsed it

自己写了一个小工具,抓取一些站点的东西,然后经过自己写的和个简单的算法, 给自己推荐感兴趣的文章,主要是在手机上看, 因为这个要抓取的 list 里面有很多国外的站点, 原因就不用说了,不用代理是访问不了的, urllib2 是支持代理的 import urllib2 enabl

自己写了一个小工具,抓取一些站点的东西,然后经过自己写的和个简单的算法,

给自己推荐感兴趣的文章,主要是在手机上看, 因为这个要抓取的 list 里面有很多国外的站点,

原因就不用说了,不用代理是访问不了的, urllib2 是支持代理的

<code>    import urllib2
    enable_proxy = True
    proxy_handler = urllib2.ProxyHandler({"http" : 'http://127.0.0.1:8087'})
    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)
    f = open('00000002.html','wb')
    html = urllib2.urlopen('http://cn.nytimes.com').read()
    f.write(html)
    f.close()
</code>
Copy after login

stack overflow urllib proxy

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template