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
。我嘗試了幾種不同的方法,但沒有運氣。任何幫助將不勝感激。
這很有道理。謝謝MD Khairul Basar。 –
@RonaldGreeff你很受歡迎。 :) –