python网络爬虫基础(利用HTMLParser)
来源:互联网 发布:七月算法 邹博 编辑:程序博客网 时间:2024/06/06 02:54
该程序爬虫对象是今日神段里的热门文章,利用HTMLParser和正则表达式
from html.parser import *import urllib.requestimport reclass Scraper(HTMLParser): def handle_starttag(self,tag,attrs): if tag=='a': attrs=dict(attrs) if(attrs.__contains__("title")): try: page={} page["link"]=attrs["href"] page["target"]=attrs["target"] page["title"]=attrs["title"] #page["article-id"]=attrs["article-id"] message.append(page) except Exception: print("捕捉错误") print(attrs)message=[]url="http://pinyin.sogou.com/zimeiti/tag/%E4%BB%8A%E6%97%A5%E7%A5%9E%E6%AE%B5"webpage=urllib.request.urlopen(url).read().decode()parser=Scraper()parser.feed(webpage)while True: index=0 for each in message: index+=1 print(r"page:%2d title:%s"%(index,each['title'])) parser.close() num=int(input("输入需要阅读的文章序号: ")) nextpage=urllib.request.urlopen("http://pinyin.sogou.com"+message[num-1]["link"]).read().decode() pat='">( )?([^a-z\nA-Z<&]*?)( )?[<br|</span>]' date=re.findall(pat,nextpage) for ts in date: if(ts[1]=='0'):break#略过广告 if(ts[1]!=''): print(ts[1]) print(''); print('') input("按下任意键返回目录界面")
获取的数据,page1-3不能打开所以会出现报错问题
HTMLPaser的基本用法介绍:
本程序中仅仅运用到handler_starttag(self,tag,attrs)
在获取到开头标记时会进入该函数,tag便是开头的字符
本程序中tag是'a'即开始为<a
attrs为该段字段dict(attrs)转换成字典,之后根据键值获取相关参数
获取基本信息后,下一个文章链接构造比较复杂所以这里使用正则表达式获取文章主体
阅读全文
1 0
- python网络爬虫基础(利用HTMLParser)
- 利用HttpClient、HtmlParser简单实现网络爬虫
- 网络爬虫---HTMLPARSER使用指南
- 网络爬虫---htmlparser
- 网络爬虫---htmlparser
- 网络爬虫---htmlparser
- Java 网络爬虫-htmlparser
- python爬虫之HTMLParser
- Python网络爬虫基础
- 利用python实现网络爬虫
- 网络爬虫---HTMLParser使用详解
- 网络爬虫---HTMLParser使用举例
- 网络爬虫---HTMLParser使用详解
- Java网络爬虫--HtmlParser应用
- [Python]利用Python进行网络爬虫
- Python基础 HTMLParser
- 利用python脚本抓取AC的代码[爬虫+HTMLParser+handle_entityref+正则表达式+模拟登陆+文件操作]
- 利用HttpClient和HtmlParser构造简单爬虫
- 重启nginx后丢失nginx.pid,如何重新启动nginx
- # Android微信支付接入
- Android Studio平台下JNI开发:入门使用及常见错误
- network switch配置
- 服务器无法继续执行该事务,此会话中的活动事务已由另外一个会话提交或终止。
- python网络爬虫基础(利用HTMLParser)
- 搜索旋转排序数组I
- CSS之Flexbox制作CSS布局易如反掌
- 用google代替CSDN的博客搜索功能
- 【R的机器学习】模型性能提升探索:神经网络
- editplus在线注册机
- MySQL 显错式注入
- Activity 自行处理运行时变更
- 网页在线时间的展示