Python-网络爬虫
来源:互联网 发布:淘宝有哪些零食店铺 编辑:程序博客网 时间:2024/06/01 08:36
Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读、易维护,并且被大量用户所欢迎的、用途广泛的语言。学习Python也有一段时间了,接下来做一下简单的爬虫程序,用来获取一些网页上的数据:
网页图:
爬取的数据:
实现代码:
#coding=utf-8import urllib.requestimport reclass Reptile(object): def start(self): # 爬取页面总数 pageCount = 10 # 页码 i = 1 while i <= pageCount: url = "http://www.neihan.net/text_%d.html"%i send_headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"} f = urllib.request.Request(url,headers=send_headers) r = urllib.request.urlopen(f) # 读取并转码 html = r.read().decode("utf-8") print("开始爬取第%d页"%i) self.doInfo(html) print("爬取完毕") i+=1 def doInfo(self, html): str_ = str(html) pattern = re.compile(r'<dd class="content"(.*?)(<br/>|</dd>)', re.S) list = pattern.findall(str_) for temp in list: self.writeToFile(temp[0]) def writeToFile(self,content): file = open("test.txt","a",encoding="utf-8") file.write(content) file.write("\n\n") file.close()if __name__ == "__main__": reptile = Reptile() reptile.start()
输出结果:
阅读全文
0 0
- python 网络爬虫代码
- Python实现网络爬虫
- Python实现网络爬虫
- Python实现网络爬虫
- python实现网络爬虫
- python网络爬虫
- python实现网络爬虫
- Python 网络爬虫
- python网络爬虫
- python 网络爬虫 北京公交
- [Python] 实现网络爬虫
- python实现网络爬虫
- Python]网络爬虫
- Python实现网络爬虫
- python 网络爬虫
- [Python] 实现网络爬虫
- Python-网络爬虫
- python 网络爬虫
- 06-图1 列出连通集 (25分)
- CodeForces
- 揭开Socket编程的面纱
- NodeMCU上手记录
- 谱聚类算法(Spectral Clustering)
- Python-网络爬虫
- EM算法(Expectation Maximization)
- 简单的基于地理图片的旅行路线还原
- 逻辑回归模型(Logistic Regression, LR)基础
- 谱聚类算法(Spectral Clustering)优化与扩展
- 在Android中使用Lambda表达式
- 矩估计法
- SpringMVC登陆小demo
- LeetCode 3. Longest Substring Without Repeating Characters