python使用ip代理抓取网页-泓源视野

python使用ip代理抓取网页

python使用ip代理抓取网页
python使用ip代理抓取网页,在抓取一个网站的信息时,如果我们进行频繁的访问,就很有可能被网站检测到而被屏蔽,解决这个问题的方法就是使用ip代理 。在我们接入因特网进行上网时,我们的电脑都会被分配一个全球唯一地ip地址供我们使用,而当我们频繁访问一个网站时,网站也正是因为发现同一个ip地址访问多次而进行屏蔽的,所以这时候如果我们使用多个ip地址进行随机地轮流访问,这样被网站检测的概率就很小了,这时候如果我们再使用多个不同的headers,这时候就有多个ip+主机的组合,访问时被发现的概率又进一步减小了。

python使用ip代理抓取网页

python使用ip代理抓取网页插图

关于代码中ip代理的使用,下面介绍一下:

步骤:
1、urllib2库中的ProxyHandler类,通过此类可以使用ip代理访问网页
proxy_support=urllib2.ProxyHandler({}),其中参数是一个字典{‘类型':'代理ip:端口号'}
2、定制、创建一个opener
opener=urllib2.build_opener(proxy_support)
3、(1)安装opener
urlib2.install_opener(opener)
(2)调用默认的opener
opener.open(url)
对于没有设置反爬虫机制的网站,我们只需要直接像上面引入ProxyHandler类进行处理,下面以访问csdn主页为例:
[python] view plain copy
import urllib
url="http://www.csdn.net/"
for i in range(0,10000):
html=urllib.urlopen(url)
print html.info()
print i
当使用上述代码时,当循环到20时,就会出现下面的错误
Traceback (most recent call last):
File "C:/Users/lenovo/PycharmProjects/untitled1/jt2/__init__.py", line 19, in
html=urllib.urlopen(url)
File "C:\Python27\lib\urllib.py", line 87, in urlopen
return opener.open(url)
File "C:\Python27\lib\urllib.py", line 213, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 350, in open_http
h.endheaders(data)
File "C:\Python27\lib\httplib.py", line 997, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 850, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 812, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 793, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
IOError: [Errno socket error] [Errno 10060]
这就是因为我们使用了计算机的单一ip进行频繁访问而被检测出来的。
下面是使用了ip代理的代码:
[python] view plain copy
import urllib.request
import random
def getHtml(url,proxies):
    random_proxy = random.choice(proxies)
    proxy_support = urllib.request.ProxyHandler({"http":random_proxy})
    opener = urllib.request.build_opener(proxy_support)
    urllib.request.install_opener(opener)
    html = urllib.request.urlopen(url)
    return html
    url = "https://byy3.com/"
    proxies = ["178.32.129.31:3128","165.225.77.44:80","105.112.8.53:3128","165.225.77.42:80","144.217.254.175:3128","134.209.69.46:8080"]
for i in range(0,10000):
    try:
      html=getHtml(url,proxies)
      print (html.info())    #打印网页的头部信息,只是为了展示访问到了网页,可以自己修改成想显示的内容
      print (i)
    except:
      print ("出现故障")
这个代码我测试是在1096次时被检测到了,要知道我的列表中只有6个ip,如果我们增加ip的个数,那么被发现的概率是不是又会更低了。对于上面的例子中的ip代理,有可能在过了一段时间后便不能用了,这个需要自己到网上搜索最新的ip代理,进行替换。还有程序中的异常处理是为了使程序能够处理ip代码访问时出现问题的情况,因为有些ip代理在访问的时候会出现故障的,这样做了可以使程序更加健壮。
对于有反爬虫机制的网页,下面还是以访问csdn中的博客为例:
[python] view plain copy
#coding:utf-8
import urllib2
import random
def get_html(url,headers,proxies):
random_userAget = random.choice(headers)
random_proxy = random.choice(proxies)
#下面是模拟浏览器进行访问
req = urllib2.Request(url)
req.add_header("User-Agent", random_userAget)
req.add_header("GET", url)
req.add_header("Host", "blog.csdn.net")
req.add_header("Referer", "http://blog.csdn.net/?&page=6")
#下面是使用ip代理进行访问
proxy_support = urllib2.ProxyHandler({"http":random_proxy})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
html = urllib2.urlopen(req)
return html
url = "http://blog.csdn.net/?&page=3"
"""
使用多个主机中的user_agent信息组成一个列表,当然这里面的user_agent都是残缺的,大家使用时可以自己找
身边的小伙伴借呦
"""
user_agents = [
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWe。。。hrome/45.0.2454.101 Safari/537.36",
"Mozilla / 5.0(Windows NT 6.1) AppleWebKit / 537.。。。。likeGecko) Chrome / 45.0.2454.101Safari/ 537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit。。。。。Gecko) Chrome/50.0.2661.102 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.3。。。。ML, like Gecko) Chrome/49.0.2623.112 Safari/537.36",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 。。。WebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
"User-Agent: Mozilla/5.0 (Windows NT 10.0) AppleWebKi。。。。。36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
"Mozilla/5.0 (Windows NT 10.0; WOW64) Apple。。。。。KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"
]
#网上的ip有可能是不能用的,需要多做尝试
myproxies=["220.189.249.80:80","124.248.32.43:80"]
html = get_html(url,user_agents,myproxies)
print html.read()
本文由 泓源视野 作者:admin 发表,其版权均为 泓源视野 所有,文章内容系作者个人观点,不代表 泓源视野 对观点赞同或支持。如需转载,请注明文章来源。
11

发表评论

Protected with IP Blacklist CloudIP Blacklist Cloud
您是第8236095 位访客, 您的IP是:[44.192.107.255]