爬取观察者网,并将爬取内容以csv格式存储
import requests
import csv
csv_file = open('articles.csv', 'w', newline='', encoding='utf-8')
writer = csv.writer(csv_file)
writer.writerow(['标题', '链接'])
url = "https://user.guancha.cn/main/search-v2"
for page in range(1, 4):
params = {'page': page, 'type': 'search_news',
'order': '1', 'keyword': '足球'}
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'}
res = requests.get(url, params=params, headers=headers)
articles = res.json()
data = articles['data']['items']
for i in data:
row = [i['title'], i['url']]
print(row)
writer.writerow(row)
csv_file.close()