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

yipeiwu_com6年前Python爬虫

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

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

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

不废话,上代码。

#!/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设计】。

相关文章

零基础写python爬虫之使用urllib2组件抓取网页内容

零基础写python爬虫之使用urllib2组件抓取网页内容

版本号:Python2.7.5,Python3改动较大,各位另寻教程。所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地。 类似于使用程序模拟IE浏览器...

python爬虫 execjs安装配置及使用

模块安装 参考官方文档安装 pip install PyExecJS 配置 该模块需要JS运行时环境 以下JS runtime经过官方测试认可,建议采用 PyV8:一...

python 每天如何定时启动爬虫任务(实现方法分享)

python2.7环境下运行 安装相关模块 想要每天定时启动,最好是把程序放在linux服务器上运行,毕竟linux可以不用关机,即定时任务一直存活; #coding:utf8 im...

利用PyCharm Profile分析异步爬虫效率详解

利用PyCharm Profile分析异步爬虫效率详解

今天比较忙,水一下 下面的代码来源于这个视频里面提到的,github 的链接为:github.com/mikeckenned…(本地下载) 第一个代码如下,就是一个普通的 for 循环...

一个月入门Python爬虫学习,轻松爬取大规模数据

一个月入门Python爬虫学习,轻松爬取大规模数据

Python爬虫为什么受欢迎 如果你仔细观察,就不难发现,懂爬虫、学习爬虫的人越来越多,一方面,互联网可以获取的数据越来越多,另一方面,像 Python这样的编程语言提供越来越多的优秀工...