python爬虫案例-爬取西刺免费代理服务器IP等信息
2017-12-02 21:33
495 查看
全栈工程师开发手册 (作者:栾鹏)
python教程全解
在使用python爬取网络数据时,网络需要设置代理服务器,防止目标网站对IP的限制。
http://www.xicidaili.com/
网站实现了众多可以使用的代理服务器,这里写了一个爬虫,爬取其中 国内高匿代理IP
使用python3.6环境
python教程全解
在使用python爬取网络数据时,网络需要设置代理服务器,防止目标网站对IP的限制。
http://www.xicidaili.com/
网站实现了众多可以使用的代理服务器,这里写了一个爬虫,爬取其中 国内高匿代理IP
使用python3.6环境
#coding:utf-8 #本实例用于获取国内高匿免费代理服务器 import urllib from bs4 import BeautifulSoup def getdata(html): #从字符串中安装正则表达式获取值 allproxy = [] # 所有的代理服务器信息 soup = BeautifulSoup(html, 'html.parser') alltr = soup.find_all("tr", class_="")[1:] #获取tr ,第一个是标题栏,去除 for tr in alltr: alltd =tr.find_all('td') oneproxy ={ 'IP地址':alltd[1].get_text(), '端口号': alltd[2].get_text(), # '服务器地址': alltd[3].a.get_text(), '是否匿名': alltd[4].get_text(), '类型': alltd[5].get_text(), '速度': alltd[6].div['title'], '连接时间': alltd[7].div['title'], '存活时间': alltd[8].get_text(), '验证时间': alltd[9].get_text(), } allproxy.append(oneproxy) alltr = soup.find_all("tr", class_="odd")[1:] # 获取tr ,第一个是标题栏,去除 for tr in alltr: alltd = tr.find_all('td') oneproxy = { 'IP地址': alltd[1].get_text(), '端口号': alltd[2].get_text(), # '服务器地址': alltd[3].a.get_text(), '是否匿名': alltd[4].get_text(), '类型': alltd[5].get_text(), '速度': alltd[6].div['title'], '连接时间': alltd[7].div['title'], '存活时间': alltd[8].get_text(), '验证时间': alltd[9].get_text(), } allproxy.append(oneproxy) return allproxy #根据一个网址,获取该网址中符合指定正则表达式的内容 def getallproxy(url='http://www.xicidaili.com/nn'): try: # 构造 Request headers agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER' headers = { # 这个http头,根据审查元素,监听发包,可以查看 "Host": "www.xicidaili.com", "Referer": "http://www.xicidaili.com/", 'User-Agent': agent } request = urllib.request.Request(url, headers=headers) # 创建一个请求 response = urllib.request.urlopen(request) # 获取响应 html = response.read() #读取返回html源码 return getdata(html) except urllib.error.URLError as e: if hasattr(e,"code"): print(e.code) if hasattr(e,"reason"): print(e.reason) return [] # allproxy = getallproxy() # print(allproxy)
相关文章推荐
- python爬虫:案例三:去哪儿酒店价格信息
- Python爬虫完整案例 - 爬取百度百科词条信息
- python爬虫之抓取代理服务器IP
- python爬虫:案例二:携程网酒店价格信息
- python爬虫抓取豆瓣所有恐怖片信息(利用多线程和构建免费ip代理池)
- 使用python为爬虫获取免费代理ip
- Python爬虫——4.4爬虫案例——requests和xpath爬取招聘网站信息
- Python2.*爬虫获取免费有效IP代理
- Python爬虫爬取智联招聘职位信息
- Python爬虫实战之爬取B站番剧信息(详细过程)
- Python爬虫框架Scrapy实战之定向批量获取职位招聘信息
- Python爬虫获取代理ip及端口
- python爬虫实战笔记---以轮子哥为起点Scrapy爬取知乎用户信息
- python爬虫实战(一)----------爬取京东商品信息
- python爬虫防止IP被封的一些措施
- python爬虫爬取艺龙国际酒店信息
- Python爬虫小实践:寻找失踪人口,爬取失踪儿童信息并写成csv文件,方便存入数据库
- Python爬虫(入门+进阶)学习笔记 1-7 数据入库之MongoDB(案例二:爬取拉勾)
- python3爬虫之IP代理
- Python3爬虫实战:爬取大众点评网某地区所有酒店相关信息