python爬虫爬取goubanjia的代理ip
来源:互联网 发布:数据库发展方向 编辑:程序博客网 时间:2024/05/16 20:51
今天这里介绍一下python3爬取http://www.goubanjia.com的代理ip的方法,这个网站的html有点变态,还做了js加密。对于初学python的我还是有一定的难道,但是研究了一段时间,写下了一个demo。接下来跟他家分享一下。
from bs4 import BeautifulSoupfrom urllib import parse,requestclass Spider: def __init__(self): self.beginPage = int(input("请输入起始页:")) self.endPage = int(input("请输入终止页:")) self.url = 'http://www.goubanjia.com/free/' self.ua_header = {"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0;"} def tiebaSpider(self): for page in range(self.beginPage, self.endPage + 1): #拼接访问的地址 myUrl = self.url +"index%d.shtml"%page self.loadPage(myUrl) # 读取页面内容 def loadPage(self, url): req = request.Request(url, headers = self.ua_header) resHtml = request.urlopen(req).read() # 解析html html=BeautifulSoup(resHtml,"lxml") #获取所以的ip的td tdResultList=html.select('td[class="ip"]') for tdResult in tdResultList: #获取当前td所以的子标签 childList= tdResult.find_all() text="" for child in childList: if 'style' in child.attrs.keys(): if child.attrs['style'].replace(' ','')=="display:inline-block;": if child.string!=None: text=text+child.string #过滤出端口号 elif 'class'in child.attrs.keys(): classList=child.attrs['class'] if 'port' in classList: port=self.get_poxy(classList[1]) #拼接端口 text=text+":"+str(port) else: if child.string != None: text = text + child.string #写入到文件 self.writeToTxt(text) #解码端口号 def get_poxy(self,port_word): word = list(port_word) num_list = [] for item in word: num = 'ABCDEFGHIZ'.find(item) num_list.append(str(num)) port = int("".join(num_list)) >> 0x3 return port def writeToTxt(self, text): txtFile = open("portFile.txt", 'a+') txtFile.write(text+"\n") txtFile.close()# 模拟 main 函数if __name__ == "__main__": # 首先创建爬虫对象 mySpider = Spider() # 调用爬虫对象的方法,开始工作 mySpider.tiebaSpider()
因为初学python代码写的还不够好,希望大家多提意见,共同学习,也希望对你有所帮助。
阅读全文
0 0
- python爬虫爬取goubanjia的代理ip
- 爬虫-爬取代理ip网页里的ip
- Java爬虫爬取代理ip
- Python爬取代理IP
- Python爬取代理IP
- Python 爬虫IP代理
- python爬虫入门(二)——爬取代理IP
- python项目之 爬取代理的ip地址
- 利用Python爬取可用的代理IP
- python IP代理爬虫,download 代理IP
- 使用阻塞队列爬取代理ip实现爬虫
- 使用阻塞队列爬取代理ip实现爬虫
- python 爬虫获取代理Ip
- 网络爬虫爬取全国省市区(动态ip代理的获取,实现对ip限制的突破)
- Python爬虫之爬取——使用代理
- 爬取代理ip
- Python爬虫IP代理池的建立和使用
- Python 爬虫 —— 爬取 IP && 快速验证
- 我这样减少了26.5M Java内存!
- UVa
- poj2892(treap)
- IT行业风投
- 2017-12-21 ubuntu first day
- python爬虫爬取goubanjia的代理ip
- 机器学习必知的15大框架
- Android界面编程之ViewSwitcher
- sscanf sprintf stringstream
- lucene&solr从入门到精通-----创建索引,写到索引库
- PAT——数字分类
- java基础学习日志--异常案例
- Java多叉树的创建,遍历,节点插入,删除,修改及节点路径获取
- C语言中 || 和 &&