批量爬取多家公司的百度新闻
其实本质修改的就是url,通过一个列表不断遍历发送给一个变量实现批量使用
import requests
import re
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
def baidu(company):
url = 'https://www.baidu.com/s?tn=news&rtt=1&bsst=1&cl=2&wd=' + company # 把链接中rtt参数换成4即是按时间排序,默认为1按焦点排序
res = requests.get(url, headers=headers).text # 加上headers用来告诉网站这是通过一个浏览器进行的访问
# print(res)
p_href = '<h3 class="news-title_1YtI1"><a href="(.*?)"'
href = re.findall(p_href, res, re.S)
p_title = '<h3 class="news-title_1YtI1">.*?>(.*?)</a>'
title = re.findall(p_title, res, re.S)
p_date = '<span class="c-color-gray2 c-font-normal">(.*?)</span>'
date = re.findall(p_date, res)
p_source = '<span class="c-color-gray c-font-normal c-gap-right">(.*?)</span>'
source = re.findall(p_source, res)
for i in range(len(title)): # range(len(title)),这里因为知道len(title) = 10,所以也可以写成for i in range(10)
title[i] = title[i].strip() # strip()函数用来取消字符串两端的换行或者空格
title[i] = re.sub('<.*?>', '', title[i]) # 核心,用re.sub()函数来替换不重要的内容
print(str(i + 1) + '.' + title[i], source[i], date[i])
print(href[i])
companys = ['阿里巴巴', '万科集团', '百度集团', '腾讯', '京东']
for i in companys: # 这个i只是个代号,可以换成其他内容
baidu(i)
print('成功!')
自动生成舆情数据报告文件
现在我们已经有了输出结果,下一步要做的就是如何保存到文件夹里啦
首先我们需要知道如何通过python生成一个文本文件:
file1= open('E:\\ceshi.txt','a')
file.write('把内容输入到txt中')
file1.close()
运行后就会发现E盘中多了一个ceshi的txt文件
使用方法:
变量名 = open(‘文件路径’,‘打开文件的模式’)
w为写模式,每次写都会清除原来的数据
a为追加模式,不会清除原来的数据
最后一定要记得使用close(),有助于释放缓存
实战代码:
import requests
import re
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
def baidu(company):
url = 'https://www.baidu.com/s?tn=news&rtt=1&bsst=1&cl=2&wd=' + company
res = requests.get(url, headers=headers).text
# print(res)
p_href = '<h3 class="news-title_1YtI1"><a href="(.*?)"'
href = re.findall(p_href, res, re.S)
p_title = '<h3 class="news-title_1YtI1">.*?>(.*?)</a>'
title = re.findall(p_title, res, re.S)
p_date = '<span class="c-color-gray2 c-font-normal">(.*?)</span>'
date = re.findall(p_date, res)
p_source = '<span class="c-color-gray c-font-normal c-gap-right">(.*?)</span>'
source = re.findall(p_source, res)
for i in range(len(title)): # range(len(title)),这里因为知道len(title) = 10,所以也可以写成for i in range(10)
title[i] = title[i].strip() # strip()函数用来取消字符串两端的换行或者空格,不过这里好像不太需要了
title[i] = re.sub('<.*?>', '', title[i]) # 核心,用re.sub()函数来替换不重要的内容
print(str(i + 1) + '.' + title[i] + '(' + date[i] + '-' + source[i] + ')')
print(href[i])
file1 = open('E:\\数据挖掘报告.txt', 'a') # 如果把a改成w的话,则每次生成txt的时候都会把原来的txt清空,用w不太好,因为这样只能保留一家公司信息;如果出现乱码问题,则设置encoding参数为utf-8,写成file1 = open('E:\\数据挖掘报告.txt', 'a',encoding='utf-8')
file1.write(company + '数据挖掘completed!' + '\n' + '\n')
for i in range(len(title)):
file1.write(str(i + 1) + '.' + title[i] + '(' + date[i] + '-' + source[i] + ')' + '\n')
file1.write(href[i] + '\n') # '\n'表示换行
file1.write('——————————————————————————————' + '\n' + '\n')
companys = ['阿里巴巴', '万科集团', '百度集团', '腾讯', '京东']
for i in companys:
baidu(i)
print(i + '百度新闻爬取成功')
print('数据获取及生成报告成功')
注意:除了使用\ \用两个斜杠取消单个反斜杠的特殊含义,也可以再文件路径的字符串钱加一个r,如open(r’E:\数据挖掘报告.txt’,‘a’)