(6-14)爬虫程序示例

文章目录

1.爬虫程序示例

import re
import requests## 从指定网页上爬取数据
from urllib.parse import quote #导入quote方法对URL中的字符进行编码
class BaiduNewsCrawler: #定义BaiduNewsCrawler类
headersParameters = { #发送HTTP请求时的HEAD信息
	 'Connection': 'Keep-Alive',
	 'Accept': 'text/html, application/xhtml+xml, */*',
	 'Accept-Language':
	'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
	 'Accept-Encoding': 'gzip, deflate',
	 'User-Agent':
	'Mozilla/6.1 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
}

def __init__(self, keyword, timeout): #定义构造方法,首先要传入关键词和超时时间
	self.url='http://news.baidu.com/ns?word='
	+ quote(keyword) + '&tn=news&from=news&cl=2&rn=20&ct=1' #要爬取的新闻网址,对keyword进行编码
	self.timeout=timeout #连接超时时间设置(单位:秒)

def GetHtml(self): #定义GetHtml方法
	request=requests.get(self.url, timeout=self.timeout,
	headers=self.headersParameters) #根据指定网址爬取网页
	self.html=request.text #获取新闻网页内容

def GetTitles(self): #定义GetTitles方法,分析网页数据,得到标题信息
	self.titles = re.findall(r'<h3 class="c-title">([\s\S]*?)</h3>',self.html) #匹配新闻标题
	for i in range(len(self.titles)): #对于每一个标题
		self.titles[i]=re.sub(r'<[^>]+>','',self.titles[i]) #去除所有HTML标记,即<...>
		self.titles[i]=self.titles[i].strip() #将标题两边的空白符去掉

def PrintTitles(self): #定义PrintTitle方法
	no=1
	for title in self.titles: #输出标题
		print(str(no)+':'+title)
		no+=1
		
if __name__ == '__main__':
bnc = BaiduNewsCrawler('南开大学',30) #创建BaiduNewsCrawler类对象
bnc.GetHtml() #获取新闻网页的内容
bnc.GetTitles() #获取新闻标题
bnc.PrintTitles() #输出新闻标题
发布了556 篇原创文章 · 获赞 140 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/u011436427/article/details/104242118