Python爬虫抓取代理IP并检验可用性的实例


本文摘自php中文网,作者不言,侵删。

这篇文章主要介绍了关于Python爬虫抓取代理IP并检验可用性的实例,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

经常写爬虫,难免会遇到ip被目标网站屏蔽的情况,银次一个ip肯定不够用,作为节约的程序猿,能不花钱就不花钱,那就自己去找吧,这次就写了下抓取 西刺代理上的ip,但是这个网站也反爬!!!

至于如何应对,我觉得可以通过增加延时试试,可能是我抓取的太频繁了,所以被封IP了。

但是,还是可以去IP巴士试试的,条条大路通罗马嘛,不能吊死在一棵树上。

不废话,上代码。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

#!/usr/bin/env python

# -*- coding:utf8 -*-

import urllib2

import time

from bs4 import BeautifulSoup

import sys

reload(sys)

sys.setdefaultencoding( "utf-8" )

req_header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',

 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',

 #'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',

 'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.3',

 'Accept-Encoding':'en-us',

 'Connection':'keep-alive',

 'Referer':'http://www.baidu.com/'

 }

req_timeout = 5

testUrl = "http://www.baidu.com/"

testStr = "wahaha"

file1 = open('proxy.txt' , 'w')

# url = ""

# req = urllib2.Request(url,None,req_header)

# jsondatas = urllib2.urlopen(req,None,req_timeout).read()

cookies = urllib2.HTTPCookieProcessor()

checked_num = 0

grasp_num = 0

for page in range(1, 160):

 req = urllib2.Request('http://www.xici.net.co/nn/' + str(page), None, req_header)

 html_doc = urllib2.urlopen(req, None, req_timeout).read()

 # html_doc = urllib2.urlopen('http://www.xici.net.co/nn/' + str(page)).read()

 soup = BeautifulSoup(html_doc)

 trs = soup.find('table', id='ip_list').find_all('tr')

 for tr in trs[1:]:

  tds = tr.find_all('td')

  ip = tds[1].text.strip()

  port = tds[2].text.strip()

  protocol = tds[5].text.strip()

  if protocol == 'HTTP' or protocol == 'HTTPS':

   #of.write('%s=%s:%s\n' % (protocol, ip, port))

   print '%s=%s:%s' % (protocol, ip, port)

   grasp_num +=1

   proxyHandler = urllib2.ProxyHandler({"http": r'http://%s:%s' % (ip, port)})

   opener = urllib2.build_opener(cookies, proxyHandler)

   opener.addheaders = [('User-Agent',

         'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36')]

   t1 = time.time()

   try:

    req = opener.open(testUrl, timeout=req_timeout)

    result = req.read()

    timeused = time.time() - t1

    pos = result.find(testStr)

    if pos > 1:

     file1.write(protocol+"\t"+ip+"\t"+port+"\n")

     checked_num+=1

     print checked_num, grasp_num

    else:

     continue

   except Exception,e:

    continue

file1.close()

print checked_num,grasp_num

个人感觉代码里没有太复杂的,就没有加注释,相信大家基本可以理解,如有问题也请多批评指正,共同进步!

相关推荐:

Python采集代理ip并判断是否可用和定时更新的方法

以上就是Python爬虫抓取代理IP并检验可用性的实例的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python输入错误怎么删除

Python函数中return后的语句执行不?

Python如何读取excel表数据

Python的用途有哪些?

Python中基本的数据结构--列表

嵌入式与Python选哪个

Python 列表推导式使用注意事项

Python--dicom图像的研究

Python实现购物车的简单实例分享

Python实现读取json文件到excel表

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...