Python基于urllib,re爬取百度的国内即时新闻
2018-01-05 15:21
441 查看
Python应用于爬虫领域业界已经相当的广泛了,今天就采用urllib + re 爬取下百度国内即时新闻。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/01/b4220fdc2785963ac11fafcb35578bd9)
软件环境:
Python : 3.6.0
PyCharm: Community 2017.2
Python 下载地址 https://www.python.org/downloads/ Pycharm 下载地址(Community是免费的) https://www.jetbrains.com/pycharm/download/#section=windows
主要思路:
采用urllib请求制定url,拿到网页的html,然后采用re进行正则匹配找到新闻标题
爬取过程:
1. 导入urllib 和 re 两个模块
2. 采用urllib.request.urlopen 打开百度信息url,并取得所有html
urllib.urlopen()方法用于打开一个url地址。
read()方法用于读取URL上的数据,并把整个页面下载下来。
3. 在Chrome中按F12可以查看到网页的源代码,可以看到新闻位于 div id="instant-news"下面
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/01/0e56d321fc0d2f4743ae3a098c3ea4fd)
4. 获取即时信息的整个div的html并存储到变量: instant_news_html
5. 从全部news的html中匹配出每一个新闻标题
![](https://oscdn.geek-share.com/Uploads/Images/Content/202005/01/7bfc7b13819ff57176a805d7fb6b3495)
完整源代码:
参考资料:
urllib
re
软件环境:
Python : 3.6.0
PyCharm: Community 2017.2
Python 下载地址 https://www.python.org/downloads/ Pycharm 下载地址(Community是免费的) https://www.jetbrains.com/pycharm/download/#section=windows
主要思路:
采用urllib请求制定url,拿到网页的html,然后采用re进行正则匹配找到新闻标题
爬取过程:
1. 导入urllib 和 re 两个模块
import urllib from urllib import request import re
2. 采用urllib.request.urlopen 打开百度信息url,并取得所有html
url = "http://news.baidu.com/guonei" response = urllib.request.urlopen(url) html = response.read().decode('utf-8')
urllib.urlopen()方法用于打开一个url地址。
read()方法用于读取URL上的数据,并把整个页面下载下来。
3. 在Chrome中按F12可以查看到网页的源代码,可以看到新闻位于 div id="instant-news"下面
4. 获取即时信息的整个div的html并存储到变量: instant_news_html
pattern_of_instant_news = re.compile('<div id="instant-news.*?</div>',re.S) instant_news_html = re.findall(pattern_of_instant_news, html)[0]
5. 从全部news的html中匹配出每一个新闻标题
pattern_of_news = re.compile('<li><a.*?>(.*?)</a></li>', re.S) news_list = re.findall(pattern_of_news, instant_news_html) for news in news_list: print(news)将会看到如入结果
完整源代码:
import urllib from urllib import request import re
url = "http://news.baidu.com/guonei" response = urllib.request.urlopen(url) html = response.read().decode('utf-8')
pattern_of_instant_news = re.compile('<div id="instant-news.*?</div>',re.S) instant_news_html = re.findall(pattern_of_instant_news, html)[0]
pattern_of_news = re.compile('<li><a.*?>(.*?)</a></li>', re.S)
news_list = re.findall(pattern_of_news, instant_news_html)
for news in news_list:
print(news)
参考资料:
urllib
re
相关文章推荐
- python基于http协议编程:httplib,urllib和urllib2
- Python学习笔记-2(urllib、re)
- python模拟浏览器打开百度首页并登录或者点击首页新闻并保存网页
- python基于http协议编程:httplib,urllib和urllib2
- Python urllib爬取百度首页
- jQuery News Ticker 基于jQuery的即时新闻行情展示插件
- python2.7 爬虫初体验爬取新浪国内新闻_20161130
- 百度拼音---基于国内最大搜索引擎百度带来的精确词库打字体验
- jQuery News Ticker 基于jQuery的即时新闻行情展示插件
- Python爬虫实例——基于urlib、urlib和re实现
- 基于文本密度的新闻正文抽取方法之Python实现
- python3 脚本爬取今日百度热点新闻并存放到mysql数据库
- python + urllib + re 爬取网易彩票大乐透开奖期号
- 【基于百度AI的人脸识别Python实现】
- 基于Scrapy框架的Python新闻爬虫
- python基于http协议编程:httplib,urllib和urllib2
- 基于jQuery实现的百度导航li拖放排列效果,即时更新数据库
- 百度转型为国内最大新闻资讯分发平台
- BoooLee pyretoolkit -- 一个基于python re模块的在线正则表达式测试工具
- python3 基于urllib模块的网络编程