簡體   English   中英

如何使用Beautiful Soup 4從網站上抓取隱藏的電話號碼

[英]How to scrape hidden phone number from website using Beautiful Soup 4

我正嘗試在http://immobilienscout.de網站上抓取一些關於房屋列表的數據。 到目前為止,我成功地刮除了所有必要的數據,只有一件事:發布代理的電話號碼。

問題是我無法理解到達文字的路徑。

例如,假設我要查找價格。 我的查找代碼如下:

HTML代碼:

<div class="is24-phone palm-hide" data-is24-phone-number-block="" data-ng-show="!showPhoneNumbers" data-position="top">
            <div class="is24-show-phone-button print-hide hide">
              <span class="fa fa-phone font-lightgray"></span>
              <a href="javascript:void(0);" class="internal-link"><font><font>Show phone number</font></font></a>
            </div>
            <div class="is24-phone-number">
              <p>
                  <span><font><font>Mobil:</font></font></span><font><font> 0162 2056442</font></font></p>
              <p>
                  <span><font><font>Phone:</font></font></span><font><font> 030 72021143</font></font></p>
              </div>
          </div>

我的代碼如下所示:

link = "https://www.immobilienscout24.de/expose/96068611"   
html = urllib2.urlopen(link)   
soup = BeautifulSoup(html, "html.parser")

findMobile = soup.find('div', attrs={'class': 'is24-phone-number'})
print findMobile.text.strip()

無輸出。 相反,我需要輸出為:0162 2056442。

有什么幫助嗎?

如果您打開頁面(例如在Chrome中),則應該能夠右鍵單擊要刮取的內容,然后點擊“檢查元素”。 然后,在再次彈出的DOM視圖中,右鍵單擊該元素,然后選擇“復制”>“復制選擇器”。 那應該給你一個css選擇器,看起來像

#sidebar > div.module.community-bulletin > div > div:nth-child(10) > div.bulletin-item-content > a

然后,您應該只需執行以下操作即可選擇該元素

soup.select("#sidebar > div.module.community-bulletin > div > div:nth-child(10) > div.bulletin-item-content > a")

編輯:這是.select()的文檔: https : .select()

這是一個例子:

>>> from bs4 import BeautifulSoup
>>> import requests
>>> r = requests.get("https://stackoverflow.com/questions/45224417/how-to-scrape-hidden-phone-number-from-website-using-beautiful-soup-4/45224481#45224481")
>>> soup = BeautifulSoup(r.text, 'html.parser')
>>> soup.select("#comment-77415832 > td.comment-text > div > span.comment-copy")
[<span class="comment-copy">I tried to use your code for the element I am interested but the output is an empty list. Any ideas how to solve this?</span>]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM