本文是维护爬虫代理IP池系列文章的第一篇文章,系列教程将会一步步教你搭建你自己的代理IP池。本文介绍了如何采集互联网上的免费的代理IP,并验证IP是否有效。
任务分析
我们爬的免费代理来自于https://www.kuaidaili.com这个网站。
用requests将ip地址与端口采集过来,将IP与PORT组合成requests需要的代理格式,用requests访问ipcheck.chinahosting.tk,并判断返回的字符串是否是代理IP,若是,则代理IP有效,若不是,则代理IP无效。
数据采集现在已经成为了基本操作了,所以大家直接看代码就可以了,注释应该写的很清楚了。如果是个新手,那么可以看这篇文章:采集wordpress并自动发布文章 ,这篇文章看懂了,基本上全世界大部分的网站你就都能爬了。
这个站点http://ipcheck.chinahosting.tk/ 是我个人搭建的用来验证IP的,详情见文章:利用虚拟主机搭建一个验证爬虫代理IP是否有效的服务 ,大家如果自己用的话最好搭建一个,基本上10多分钟就能搭建完,并且只要点点鼠标。 ### 代码实现
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 62 63 64 65 66 67 68 69
| import gevent.monkey gevent.monkey.patch_socket() import gevent import requests import time from fake_useragent import UserAgent from lxml import etree from bs4 import BeautifulSoup import sys reload(sys) sys.setdefaultencoding('utf8')
class GetProxy(): def __init__(self): self.ua = UserAgent() self.check_url = 'http://ipcheck.chinahosting.tk/' self.threads = [] self.count = 0 def download_page(self, url): headers = {"User-Agent":self.ua.random} response = requests.get(url) print response.status_code return response.content def crawl_kuaidaili(self): for page in xrange(1,50): url = 'https://www.kuaidaili.com/free/inha/' + str(page) response = self.download_page(url) soup = BeautifulSoup(response, "html.parser") all_tr = soup.find_all('tr') for tr in all_tr: ip = tr.find('td',attrs={"data-title":"IP"}) port = tr.find('td',attrs={"data-title":"PORT"}) if ip==None or port==None: pass else: self.threads.append(gevent.spawn(self.valid_check, [ip.get_text(), port.get_text()])) time.sleep(1)
def valid_check(self, *arg): ip = arg[0][0] port = arg[0][5] proxyip = "http://"+ip+":"+port proxy={"http":proxyip} try: response = requests.get(self.check_url, proxies=proxy, timeout=5) if str(response.content) == ip: print ip self.count = self.count + 1 else: pass except: pass
def start(self): self.crawl_kuaidaili() gevent.joinall(self.threads)
|