python爬虫今日头条热榜前十新闻
时间: 2023-12-25 12:04:40 浏览: 138
Python今日头条爬虫.zip
以下是使用Python爬虫获取今日头条热榜前十新闻的示例代码:
```python
import requests
import json
import xlwt
# 发送请求获取热榜数据
url = 'https://www.toutiao.com/api/pc/hot_gallery/?widen=1'
response = requests.get(url)
json_data = json.loads(response.text)
# 创建Excel文件和工作表
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('热榜新闻')
# 写入表头
worksheet.write(0, 0, '标题')
worksheet.write(0, 1, '链接')
# 获取热榜新闻的标题和链接
title_list = []
for data in json_data['data']:
title = data['Title']
url = data['Url']
title_list.append(title)
# 写入Excel
row = len(title_list)
worksheet.write(row, 0, title)
worksheet.write(row, 1, url)
# 保存Excel文件
workbook.save('hot_news.xls')
# 打印热榜新闻标题
for title in title_list[:10]:
print('热榜标题:', title)
```
阅读全文