2017-06-21 76 views
1

按標題錨標籤中提取多個屬性的單個屬性,這是我的代碼:使用BeautifulSoup

import bs4 
import re 
from urllib.request import urlopen as uReq 
from bs4 import BeautifulSoup as soup 

my_url = "https://finance.yahoo.com/screener/predefined/undervalued_growth_stocks" 

loadpage = uReq(my_url) 
showloadpage = loadpage.read() 
loadpage.close() 

soupit = soup(showloadpage, "html.parser") 

#the regex works - returns all of the "tr" tags, i.e. containers 
containers = soupit.findAll("tr", {"class" : re.compile("data-row.*")}) 


for container in containers: 
    container.findAll("a", {"class" : "Fw(b)"}) 

我得到的結果是:

[<a class="Fw(b)" data-reactid="69" data-symbol="AMAT" href="/quote/AMAT?p=AMAT">AMAT</a>] 
[<a class="Fw(b)" data-reactid="99" data-symbol="MS" href="/quote/MS?p=MS">MS</a>] 
[<a class="Fw(b)" data-reactid="129" data-symbol="NLY" href="/quote/NLY?p=NLY">NLY</a>] 
[<a class="Fw(b)" data-reactid="159" data-symbol="ODP" href="/quote/ODP?p=ODP">ODP</a>] 
[<a class="Fw(b)" data-reactid="189" data-symbol="FCAU" href="/quote/FCAU?p=FCAU">FCAU</a>] 
[<a class="Fw(b)" data-reactid="219" data-symbol="RDC" href="/quote/RDC?p=RDC">RDC</a>] 
[<a class="Fw(b)" data-reactid="249" data-symbol="ING" href="/quote/ING?p=ING">ING</a>] 
[<a class="Fw(b)" data-reactid="279" data-symbol="FTI" href="/quote/FTI?p=FTI">FTI</a>] 
[<a class="Fw(b)" data-reactid="309" data-symbol="BX" href="/quote/BX?p=BX">BX</a>] 
[<a class="Fw(b)" data-reactid="339" data-symbol="FNSR" href="/quote/FNSR?p=FNSR">FNSR</a>] 

我我試圖得到的是data-symbol屬性現在,但最終我也想要href。我嘗試了幾種不同的方法,但沒有運氣。任何幫助將不勝感激。

回答

0

只需遍歷您爲每個容器找到的鏈接並獲取所需的值即可。 linkdictionary,因此如果您不想使用get()方法,則可以使用屬性名稱作爲關鍵字輕鬆獲取值,如link["href"]
嘗試lxml解析器,它比html.parser快很多。

import re 
from urllib.request import urlopen as uReq 
from bs4 import BeautifulSoup as soup 

my_url = "https://finance.yahoo.com/screener/predefined/undervalued_growth_stocks" 

loadpage = uReq(my_url) 
showloadpage = loadpage.read() 
loadpage.close() 

soupit = soup(showloadpage, "lxml") 

#the regex works - returns all of the "tr" tags, i.e. containers 
containers = soupit.find_all("tr", {"class" : re.compile("data-row.*")}) 

for container in containers: 
    links = container.find_all("a", {"class" : "Fw(b)"}) 
    for link in links: 
     data_symbol = link.get("data-symbol") 
     href = link.get("href") 
     print(data_symbol, href) 
+0

這很有道理。謝謝MD Khairul Basar。 –

+0

@RonaldGreeff你很受歡迎。 :) –