使用Python的网页刮取数据
问题描述:
我刚开始学习使用Python的网页抓取。 我的目标是网站从http://money.rediff.com/companies/Bajaj-Auto-Ltd/10540026报废Bajaj汽车有限公司的实时新闻。使用Python的网页刮取数据
问题:我无法提取内容(即新闻)。
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’对象有没有属性‘内容’” 我没有得到期望的结果。
任何输入将不胜感激。
答
您正试图让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...
貌似找不到你的想法是存在的。尝试打印'li',看看里面是否真的有'a' –