[英]Scrape data middle of <b> and <br> tag using BeautifulSoup
HTML如下所示:
<td>
<font face="Arial, sans-serif" size="-1">
<b>Home Phone: </b>507-383-1070<br>
<b>Cell Phone: </b>507-383-1070<br>
<b>E-Mail: </b><a href=mailto:macehrhardt@gmail.com>macehrhardt@gmail.com</a><br>
</font>
</td>
我只想抓取例如Home Phone
和Cell Phone
數據。 507-383-1070
。 您能幫我這個忙嗎,我將如何使用BeautifulSoup解決這個問題。 我嘗試了多種方法,但沒有找到任何方法。
您可以使用帶有正則表達式的soup.find_all
。
>>> soup.find_all(text=re.compile('\d+(-\d+){2}'))
['507-383-1070', '507-383-1070']
您可能要調整正則表達式,具體取決於要提取的電話號碼的格式。
對於您提供的HTML,可以如下提取它們:
from bs4 import BeautifulSoup
html = """<td>
<font face="Arial, sans-serif" size="-1">
<b>Home Phone: </b>507-383-1070<br>
<b>Cell Phone: </b>507-383-1070<br>
<b>E-Mail: </b><a href=mailto:macehrhardt@gmail.com>macehrhardt@gmail.com</a><br>
</font>
</td>"""
soup = BeautifulSoup(html, "html.parser")
entries = [b.next.next for b in soup.find_all('b')][:2]
print entries
給你:
[u'507-383-1070', u'507-383-1070']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.