Python 爬虫 —— 爬取 IP && 快速验证
来源:互联网 发布:网络答题知识竞赛答案 编辑:程序博客网 时间:2024/04/29 21:46
本作品采用知识共享署名 4.0 国际许可协议进行许可。
使用的是西刺免费代理IP的 IP 资源,参考了python爬虫-爬取代理IP并通过多线程快速验证
CODE:
获取 IP
import requestsfrom bs4 import BeautifulSoup# get proxiesfor 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 reimport requestsimport threadingsrcfile = 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 echildthread = []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()
0 0
- Python 爬虫 —— 爬取 IP && 快速验证
- python爬虫入门(二)——爬取代理IP
- Python爬虫——小说爬取
- python爬虫爬取goubanjia的代理ip
- 【Python网络爬虫学习01】爬取网络验证码
- Python爬虫教程—python爬取糗事百科详解
- Python爬虫爬取百度搜索结果——邮箱地址
- Python网络爬虫——爬取POJ题目
- Python—爬虫—简单爬取豆瓣电影
- Python爬虫——爬取网站的图片
- Python爬虫——爬取网页中的图片小试牛刀
- Python爬虫(一)——爬取糗事百科
- (6)Python爬虫——爬取中新网新闻
- Python 爬虫实例——网页图片爬取
- Python爬虫之——爬取妹子图片
- Python爬虫之爬取——使用代理
- Python爬虫之爬取——限制下载速度
- Python爬虫——模拟登陆爬取csdn页面
- 跨服务器的复制操作
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
- 子线程和子线程之间的通信
- ajax-review
- django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.的解决办法
- Python 爬虫 —— 爬取 IP && 快速验证
- 链表基础操作
- Ceph添加mon节点
- 二叉搜索树与双向链表
- POJ:3050 Hopscotch(DFS+set)
- springmvc+mybatis下载项目自带模板
- 微信支付开发教程JAVA编[008]-wxsdk4java:微信公众号和微信支付开发的所有API功能点的封装及调用实例
- struts 2 global-results 全局结果集
- poj-Cable master(double预处理为int型)