![](/img/trans.png)
[英]I am not able to create test for given code in pycharm. Is anyone can help me for this?
[英]i am unable to scrape the phone number . Can anyone help me
這是該部分的 html 內容。
<p class="contact-info " onclick="_ct('clntphn', 'lspg');">
<i class="contactNo spriteImg"></i>
<span><a><b>+(91)-80-30805680</b></a></span>
</p>
我嘗試通過以下方式提取它:
soup.find('p',{'class':'contact-info'})
但徒勞無功。 它無法做同樣的事情。
您可以使用帶有標簽的find
方法:
from bs4 import BeautifulSoup as soup
s = soup(html_data, "lxml")
number = s.find('b').text
輸出:
u'+(91)-80-30805680'
要獲得第一個實例,您可以嘗試以下操作:
new_s = s.findAll("p", {"class":"contact-info"})
new_data = [i.text for i in new_s]
print(new_data[0].replace("\n", ''))
輸出:
+(91)-80-30805680
您需要將您的 find 方法加入鏈中:
soup.find('p',{'class':'contact-info'}).find('b').text
我會使用 CSS 選擇器。 由於它周圍有一些空白,我們可以輕松地剝離它。
soup.select_one('p.contact-info').get_text(strip=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.