请为以下代码每行代码作注释 import requests import re headers ={ 'User-Agent': 'Mz1lla/5.e (Mindows NT 10.8; win64; x64) Chrome/69.0.3497.100 Safani/537.36'} def baidu(company,page): num = (page - 1) * 18 url = 'https://www.baidu.com/s?tn=news&rtt=4&bsst=1&cl=2&wd'+ company +'&pn='+ str(num) res = requests.get(url, headers =headers).text p_info = '<p class="c-author">(.*?)</p>' info = re.findall(p_info,res, re.S) p_href = '<h3 class="c-title">.*?<a href="(.*?)"' href = re.findall(p_href,res, re.S) p_title = '<h3 class="c-title“>.*?>(.*?)</a>' title = re.findall(p_title,res, re.S) source = [] date = [] for i in range(len(info): title[i] = title[i].strip() title[i] = re.sub('<.*?>,,title[i]) info[i] = re.sub('<.*?>', info[i]) source.append(info[i].split(' ')[0]) date.append(info[i].split( ' ')[1]) source[i] = source[i].strip() date[i] =date[i].strip() print(str(i + 1)+'.'+ title[i]+'('+ date[i] +'-'+ source[i] + ')') print(href[i]) companys = ['华能信托', '阿里巴巴', '万科集团', '百度集团', '腾讯','京东'] for company in companys: for i in range(20): baidu(company,i+1) print(company+'第'+str(i+1)+'页爬取成功')
时间: 2024-04-27 22:19:50 浏览: 90
```python
# 导入requests和re模块
import requests
import re
# 设置请求头
headers ={ 'User-Agent': 'Mz1lla/5.e (Mindows NT 10.8; win64; x64) Chrome/69.0.3497.100 Safani/537.36'}
# 定义一个函数,用于爬取百度新闻搜索结果
def baidu(company,page):
# 根据页码计算出相应的条目数
num = (page - 1) * 18
# 构造请求URL
url = 'https://www.baidu.com/s?tn=news&rtt=4&bsst=1&cl=2&wd'+ company +'&pn='+ str(num)
# 发送GET请求,并获取响应内容
res = requests.get(url, headers =headers).text
# 提取新闻信息
p_info = '<p class="c-author">(.*?)</p>'
info = re.findall(p_info,res, re.S)
# 提取新闻链接
p_href = '<h3 class="c-title">.*?<a href="(.*?)"'
href = re.findall(p_href,res, re.S)
# 提取新闻标题
p_title = '<h3 class="c-title“>.*?>(.*?)</a>'
title = re.findall(p_title,res, re.S)
# 存储新闻来源和发布时间
source = []
date = []
for i in range(len(info)):
title[i] = title[i].strip() # 去除标题中的空格
title[i] = re.sub('<.*?>', '',title[i]) # 去除标题中的HTML标签
info[i] = re.sub('<.*?>', '', info[i]) # 去除信息中的HTML标签
source.append(info[i].split(' ')[0]) # 提取新闻来源
date.append(info[i].split( ' ')[1]) # 提取新闻发布时间
source[i] = source[i].strip() # 去除来源中的空格
date[i] =date[i].strip() # 去除时间中的空格
# 打印新闻标题、发布时间、来源、链接
print(str(i + 1)+'.'+ title[i]+'('+ date[i] +'-'+ source[i] + ')')
print(href[i])
# 定义一个列表,存储要搜索的公司名称
companys = ['华能信托', '阿里巴巴', '万科集团', '百度集团', '腾讯','京东']
# 循环遍历每个公司名称,并爬取前20页的新闻搜索结果
for company in companys:
for i in range(20):
baidu(company,i+1)
print(company+'第'+str(i+1)+'页爬取成功')
```
阅读全文