使用python进行Web数据抓取

2024-04-29 14:27:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始用Python学习web抓取。 我的目标是从http://money.rediff.com/companies/Bajaj-Auto-Ltd/10540026为巴贾杰汽车有限公司提供即时新闻。

问题是:我无法提取内容(即新闻)。

from urllib.request import urlopen
from bs4 import BeautifulSoup

url = 'http://money.rediff.com/companies/Bajaj-Auto-Ltd/10540026'
data = urlopen(url)
soup = BeautifulSoup(data)

te=soup.find('a',attrs={'target':'_jbpinter'})
lis=te.find_all_next('a',attrs={'target':'_jbpinter'})
#print(lis)

for li in lis:
    print(li.find('a').contents[0])

我收到错误“AttributeError:'NoneType'对象没有属性'contents'” 我没有得到想要的结果。

如有任何意见,我们将不胜感激。


Tags: fromimportcomhttpurlautofindurlopen
1条回答
网友
1楼 · 发布于 2024-04-29 14:27:44

您试图获取a标记两次。在

更换

for li in lis:
    print(li.find('a').contents[0])

^{pr2}$

你可以得到这样的输出:

Need Different Rates For Different Products: Rahul Bajaj on GST
Reforms irrespective of Bihar results: Bajaj
Auto shares in focus; Tata Motors up over 5%
We believe new Avenger will stimulate the market: Bajaj Auto's Eric Vas
BHP Billiton pins future of Indonesian coal mine on new...

相关问题 更多 >