Python 爬取新浪网新闻和存取CSV文件

import requests
import csv
from bs4 import BeautifulSoup
headers={“user-agent”:“Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36”}
proxy={“http”:“218.60.8.83:3129”,“http”:“113.78.67.113:9797”}
res=requests.get(“https://news.sina.com.cn/china/”,headers=headers,proxies=proxy)
res.encoding=“utf-8”
soup=BeautifulSoup(res.text,“html.parser”)
Big_News=[]
for news in soup.select(".news-2 li a"):
site=news[“href”]
tittle=news.text
Big_News.append([tittle, site])
print(site,tittle)
with open(“news.csv”,“w”,newline="") as fp:
write_flie=csv.writer(fp)
head=[“新闻标题”,“新闻链接”]
write_flie.writerow(head)
for rows in Big_News:
write_flie.writerow(rows)
fp.close()
Python 爬取新浪网新闻和存取CSV文件
我们的想法就是获取页面中的新闻标题和新闻链接,再存储到CSV文件中。
Python 爬取新浪网新闻和存取CSV文件
这是文件运行后的结果,多条记录都依次写入成功。
Python 爬取新浪网新闻和存取CSV文件