我正在尝试解析一组列出有关智能手机移动设备的信息的表。例如this link。我正在尝试获取4个我需要的特定字段,而获取第四个字段会让我抓狂。
看起来HTML的格式很糟糕。我们将几个表按顺序放入html中。前5个是可以的,但是第六个表以</td></tr></table>结束,关闭了一个<td>和一个<tr>,这两个之前没有打开过(或者至少我认为这是问题所在):
<table cellspacing="0">
<tr>
<th rowspan="5" scope="row">Memory</th>
<td class="ttl"><a href="glossary.php3?term=memory-card-slot">Card slot</a></td>
<td class="nfo" data-spec="memoryslot">microSD, up to 256 GB (uses shared SIM slot)</td></tr>
<tr>
<td class="ttl"><a href="glossary.php3?term=dynamic-memory">Internal</a></td>
<td class="nfo" data-spec="internalmemory">64GB 6GB RAM, 128GB 6GB RAM, 128GB 8GB RAM, 256GB 8GB RAM</td>
</tr>
<tr><td class="ttl"> </td><td class="nfo" data-spec="memoryother">UFS2.1</td></tr>
</td>
</tr>
</table>此外,第七个表的列表也很糟糕,但我想这对bs4来说应该不是问题。
因此,如果我尝试使用CSS选择器来获取从表7到最后一个表中的任意值,选择器将返回None。实际上,如果我只是使用一个选择器来获取所有的表,它只会选择前6个表:
dsoup = BeautifulSoup(dr.content, 'html.parser')
dsel = dsoup.select('#specs-list > table')
print('Found {} tables'.format(len(dsel))) # Prints 6 tables
dsel = dsoup.select_one('#specs-list > table:nth-of-type(10) > tbody > tr:nth-of-type(3) > td.nfo')
print(dsel.text.split('\n')) # None所以问题是,有没有办法解析像这样格式不好的HTML,或者这是不可能的?
发布于 2019-12-12 00:53:18
不要使用'html.parser',但'html5lib' - it会根据(大多数) HTML5规则进行解析:
import requests
from bs4 import BeautifulSoup
url = 'https://www.gsmarena.com/xiaomi_redmi_note_8_pro-9812.php'
soup = BeautifulSoup(requests.get(url).text, 'html5lib')
for th in soup.select('#specs-list th'):
table = th.find_previous('table')
for ttl in table.select('.ttl'):
print('{:<20} {:<20} {}'.format( th.text, ttl.text, ttl.find_next_sibling('td', {'class':'nfo'}).get_text(strip=True, separator=' ')) )打印:
Network Technology GSM / HSPA / LTE
Network 2G bands GSM 850 / 900 / 1800 / 1900 - SIM 1 & SIM 2
Network 3G bands HSDPA 850 / 900 / 1900 / 2100
Network 4G bands LTE band 1(2100), 3(1800), 5(850), 7(2600), 8(900), 40(2300), 41(2500)
Network Speed HSPA 42.2/5.76 Mbps, LTE-A
Launch Announced 2019, August
Launch Status Available. Released 2019, September
Body Dimensions 161.4 x 76.4 x 8.8 mm (6.35 x 3.01 x 0.35 in)
Body Weight 200 g (7.05 oz)
Body Build Front/back glass (Gorilla Glass 5)
Body SIM Hybrid Dual SIM (Nano-SIM, dual stand-by)
Display Type IPS LCD capacitive touchscreen, 16M colors
Display Size 6.53 inches, 104.7 cm 2 (~84.9% screen-to-body ratio)
Display Resolution 1080 x 2340 pixels, 19.5:9 ratio (~395 ppi density)
Display Protection Corning Gorilla Glass 5
Display 500 nits max brightness HDR
Platform OS Android 9.0 (Pie); MIUI 10
Platform Chipset Mediatek Helio G90T (12nm)
... and so on.https://stackoverflow.com/questions/59290320
复制相似问题