我刚刚开始学习使用Python进行网络抓取。我的目标是从http://money.rediff.com/companies/Bajaj-Auto-Ltd/10540026网站上为Bajaj汽车有限公司收集实时新闻。
问题是:我无法提取内容(即新闻)。
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'“,也没有得到想要的结果。
如有任何意见,将不胜感激。
发布于 2015-11-04 16:52:11
您正在尝试两次获取a
标记。
替换
for li in lis:
print(li.find('a').contents[0])
使用
for li in lis:
print(li.get_text())
你得到了这个输出:
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...
https://stackoverflow.com/questions/33527206
复制相似问题