8 批量获取多家公司的新闻并生成数据报告

批量爬取多家公司的百度新闻

其实本质修改的就是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’)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啥都鼓捣的小yao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值