Python 爬虫 —— 爬取 IP && 快速验证
2017-03-22 20:20
363 查看
本作品采用知识共享署名 4.0 国际许可协议进行许可。
使用的是西刺免费代理IP的 IP 资源,参考了python爬虫-爬取代理IP并通过多线程快速验证
CODE:
获取 IP
import requests from bs4 import BeautifulSoup # get proxies for page in range(1,50): url = 'http://www.xicidaili.com/nn/%s' %page user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" headers = {'user-agent': user_agent} r = requests.get(url, headers = headers) print 'opening %s\n' % url soup = BeautifulSoup(r.content, 'lxml') # print soup.prettify() trs = soup.find('table', id = 'ip_list').findAll('tr') # print trs[1] for tr in trs[1:]: tds = tr.findAll('td') ip = tds[1].text.strip() port = tds[2].text.strip() with open('proxies.txt', 'a') as f: f.write('http://%s:%s\n' % (ip, port)) print 'Adding http://%s:%s to table' % (ip, port)
验证
import re import requests import threading srcfile = open('proxies.txt', 'r') outfile = open('verified.txt', 'w') url = 'http://pv.sohu.com/cityjson?ie=utf-8' user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0" headers = {'user-agent': user_agent} mutex = threading.Lock() def verify(): try: mutex.acquire() proxy = srcfile.readline().strip() mutex.release() print proxy proxies = {'http': proxy} r = requests.get(url, proxies = proxies, headers = headers, timeout = 5) print r.content mutex.acquire() outfile.write('%s\n' %proxy) mutex.release() except requests.RequestException, e: print e childthread = [] for i in range(4900): t = threading.Thread(target = verify) childthread.append(t) t.start() for t in childthread: t.join() srcfile.close() outfile.close()
相关文章推荐
- 通过Python爬虫代理IP快速增加博客阅读量
- c# 快速验证代理IP是否有用
- c# 快速验证代理IP是否有用
- Python的网页爬虫&文本处理&科学计&机器学习&数据挖掘工具集
- windows-python爬虫之scrapy快速安装
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- c# 使用HttpWebRequest,HttpWebResponse 快速验证代理IP是否有用
- 【python】简单爬虫制作(汽车之家&易车网报价爬虫)
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- python IP代理爬虫,download 代理IP
- Python快速学习01:Eclipse上配置PyDev & 'Hello World !'
- Python快速学习03:运算 & 缩进和选择
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- C# 使用HttpWebRequest,HttpWebResponse 快速验证代理IP是否有用
- Python快速学习02:基本数据类型 & 序列
- Python快速学习03:运算 & 缩进和选择
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器库
- c# 快速验证代理IP是否有用