意圖 我正在努力從Wikipedia 中提取有關所有國家/地區的資料。我希望我的決議器足夠通用以適用于所有國家/地區。
假設我現在正在從所有國家/地區提取 GDP (PPP)。在Wikipedia 中,它們被放置在 infoBox 表中。問題是 GDP(PPP) 在表中的 3 個不同行之間拆分。
這是結構:
<th scope="row" class="infobox-label">
<a href="/wiki/Gross_domestic_product" title="Gross domestic product">GDP</a> 
<style data-mw-deduplicate="TemplateStyles:r886047488">.mw-parser-output .nobold{font-weight:normal}</style>
<span class="nobold">(<a href="/wiki/Purchasing_power_parity" title="Purchasing power parity">PPP</a>)</span>
</th>
<td class="infobox-data">2020 estimate</td>
</tr>
<tr class="mergedrow">
<th scope="row" class="infobox-label">
<div class="ib-country-fake-li">? Total</div>
</th>
<td class="infobox-data"><img alt="Increase" src="//upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Increase2.svg/11px-Increase2.svg.png" decoding="async" title="Increase" width="11" height="11" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Increase2.svg/17px-Increase2.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Increase2.svg/22px-Increase2.svg.png 2x" data-file-width="300" data-file-height="300" /> $1.391 trillion<sup id="cite_ref-IMFWEOEG_10-0" class="reference"><a href="#cite_note-IMFWEOEG-10">[10]</a></sup> (<a href="/wiki/List_of_countries_by_GDP_(PPP)" title="List of countries by GDP (PPP)">20th</a>)</td>
</tr>
<tr class="mergedbottomrow">
<th scope="row" class="infobox-label">
<div class="ib-country-fake-li">? Per capita</div>
</th>
<td class="infobox-data"><img alt="Increase" src="//upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Increase2.svg/11px-Increase2.svg.png" decoding="async" title="Increase" width="11" height="11" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Increase2.svg/17px-Increase2.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Increase2.svg/22px-Increase2.svg.png 2x" data-file-width="300" data-file-height="300" /> $14,023<sup id="cite_ref-IMFWEOEG_10-1" class="reference"><a href="#cite_note-IMFWEOEG-10">[10]</a></sup> (<a href="/wiki/List_of_countries_by_GDP_(PPP)_per_capita" title="List of countries by GDP (PPP) per capita">92nd</a>)</td>
</tr>
這是我到目前為止嘗試過的:
site= "http://en.wikipedia.org/wiki/Brazil"
country = requests.get(site)
countryPage = BeautifulSoup(country.content, "html.parser")
infoBox = countryPage.find("table", class_="infobox ib-country vcard")
#find GDP PPP
tds = infoBox.select('th:-soup-contains("PPP") tr')
print(tds)
問題 盡管使用“ tr”作為 CSS 選擇器,該代碼仍列印 GDP PPP 本身的行,而不是其后的行。
誰能告訴我我做錯了什么?如何在使用 CSS 選擇器找到的行之后選擇表格行?
uj5u.com熱心網友回復:
要選擇下一個兄弟姐妹,<tr>您可以使用:
soup.select_one('tr:has(th:-soup-contains("PPP"))~tr')
或者你想要它們兩個:
soup.select('tr:has(th:-soup-contains("PPP"))~tr')[:2]
獲取文本:
[x.text for x in soup.select('tr:has(th:-soup-contains("PPP"))~tr')[:2]]
uj5u.com熱心網友回復:
我不認為你可以用 css 選擇器實作你想要的。您必須以一種或另一種方式存盤行或獲取行的索引。如果將select結果轉換為生成器,則可以使用next
trs = (tr for tr in soup.select('tr'))
for tr in trs:
if 'PPP' in tr.text:
print(next(trs).text)
print(next(trs).text)
>>> ? Total $3.328 trillion[8] (8th)
>>> ? Per capita $15,642[8] (84th)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/347508.html
標籤:Python 网页抓取 美汤 css-选择器 维基百科
上一篇:努力用美湯獲得干凈的卓越
下一篇:為什么網頁抓取回圈回傳錯誤
