python爬虫爬取goubanjia的代理ip
2017-12-21 21:34
393 查看
今天这里介绍一下python3爬取http://www.goubanjia.com的代理ip的方法,这个网站的html有点变态,还做了js加密。对于初学python的我还是有一定的难道,但是研究了一段时间,写下了一个demo。接下来跟他家分享一下。
因为初学python代码写的还不够好,希望大家多提意见,共同学习,也希望对你有所帮助。
from bs4 import BeautifulSoup from urllib import parse,request class 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代码写的还不够好,希望大家多提意见,共同学习,也希望对你有所帮助。
相关文章推荐
- python3爬虫伪装代理IP
- Python爬虫获取代理ip及端口
- Python爬虫抓取代理IP并检验可用性的实例
- Python爬虫设置代理IP爬取知乎图片
- python爬虫入门(二)——爬取代理IP
- Python爬虫抓取代理IP并检验可用性
- Python爬虫设置代理IP和伪装成浏览器的方法
- Python2.*爬虫获取免费有效IP代理
- Python 爬虫入门—— IP代理使用
- Python爬虫实战——代理IP全部抓取
- python 网络爬虫——请求头,ip代理
- Python 爬虫入门(二)—— IP代理使用
- python 爬虫 批量获取代理ip
- python爬虫的代理IP设置
- 使用python为爬虫获取免费代理ip
- Python实现爬虫设置代理IP和伪装成浏览器的方法分享
- 【Python3.6爬虫学习记录】(十一)使用代理IP及用多线程测试IP可用性--刷访问量
- 干货|Python爬虫如何设置代理IP
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- 鱼c笔记——Python爬虫(三):使用User Agent和代理IP隐藏身份