Python-ulsoup问题解析选项卡

2024-05-16 14:40:46 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在使用beautifulsoup解析以下网站中的表,但并不是所有的行都返回。我正在寻找文章标签(http://itp.ne.jp/result/?kw=%92J%98e%8E%95%89%C8%83N%83%8A%83j%83b%83N

url = 'http://itp.ne.jp/result/?kw=%92J%98e%8E%95%89%C8%83N%83%8A%83j%83b%83N'
page = requests.get(url)
prefsoup = BeautifulSoup(page.content,"html.parser")

art= prefsoup.find_all("article")

print(art)

[<article>
<section class="noimage">
<h4 class="clearfix">
<a class="blackText" href="/shop/KN0114031400001406/" target="_blank">谷脇歯科クリニック</a>
<a class="itrademark24" href="/stats_click/?s_bid=KN0114031400001406&amp;s_sid=FSP-LSR-001&amp;s_fr=V09&amp;s_ck=C12&amp;s_acd=7" target="_blank"><img alt="付加価値情報" src="/img/pc/shop/icon_itrade_7.gif"/></a>
</h4>
<p><span class="inlineSmallHeader">住所</span> 〒060-0042 北海道札幌市中央区大通西5丁目 <a class="boxedLink navigationLink" href="/shop/KN0114031400001406/map.html" target="_blank">地図・ナビ</a></p>
<p><span class="inlineSmallHeader">TEL</span>
<a class="whiteboxicon popup_04" href="/guide/phonemark.html">(代)</a>
<b>011-213-1184</b></p>
<p>
<span class="inlineSmallHeader">URL</span>
http://taniwaki-dental.com</p></section></article>]

但是,它缺少电子邮件信息的最后一段

<p><span class="inlineSmallHeader">EMAIL</span>
taniwaki@kzh.biglobe.ne.jp<!-- br-->            
</p>

此外,len(art)返回2,art[1]返回索引超出范围的错误。你知道吗

尝试了几页,得到了相同的问题。你知道吗


Tags: httptargethtmlarticleshopclassamphref
1条回答
网友
1楼 · 发布于 2024-05-16 14:40:46

使用解析器html5lib而不是html.parser,它将像一个符咒一样工作。您只需更改以下代码行-

prefsoup = BeautifulSoup(page.content,"html.parser")

至-

prefsoup = BeautifulSoup(page.content,"html5lib")

当然,您需要使用pip install html5lib安装html5lib。你知道吗

也检查一下-https://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser

相关问题 更多 >