Python基于urllib,re爬取百度的国内即时新闻

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基于urllib,re爬取百度的国内即时新闻相关的知识,希望对你有一定的参考价值。

Python应用于爬虫领域业界已经相当的广泛了,今天就采用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基于urllib,re爬取百度的国内即时新闻的主要内容,如果未能解决你的问题,请参考以下文章

python爬取百度贴吧文字内容

Python urllib爬取百度首页

翻页式爬取百度图片

爬取翻页流的百度图片

python爬取花瓣网任意面板图片

Python爬虫之简单的爬取百度贴吧数据