用天行数据获取今日头条的前100条数据,保存至excel
from datetime import datetime
import openpyxl
import requests
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.append(('时间', '标题', '链接', '来源'))
for page in range(1, 6):
resp = requests.get(
url='http://api.tianapi.com/topnews/index',
params={
'key': '请求参数的参数值',
'page': page,
'num': 20
}
)
result = resp.json()
for news_dict in result['newslist']:
ctime, title, url, source = news_dict['ctime'], news_dict['title'], news_dict['url'], news_dict['source']
sheet.append((ctime, title, url, source))
curr = datetime.now()
workbook.save(f'今日头条新闻数据_{curr.year}-{curr.month:0>2d}-{curr.day:0>2d}.xlsx')
|