簡體   English   中英

使用python抓取網頁數據

[英]Web Scraping data using python

我剛開始使用 Python 學習網頁抓取。 我的目標是從http://money.rediff.com/companies/Bajaj-Auto-Ltd/10540026網上抓取 Bajaj Auto Ltd. 的實時新聞。

問題:我無法提取內容(即新聞)。

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' object has no attribute 'contents'” 我沒有得到想要的結果。

任何輸入將不勝感激。

您正試圖兩次獲取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...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM