Python BeautifulSoup采集新闻

python使用BeautifulSoup采集新浪新闻

from bs4 import BeautifulSoup
import requests
url="http://roll.finance.sina.com.cn/finance/zq1/ssgs/index.shtml"
res=requests.get(url)
res.encoding='gb2312'
soup=BeautifulSoup(res.text,'html.parser')
for news in soup.select('.list_009 li'):#爬取新闻列表
    title=news.select('a')[0].text#新闻标题
    time=news.select('span')[0].text#时间
    print(title,time)

采集结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/tmaczt/article/details/82685128