簡體   English   中英

使用BeautifulSoup進行網頁抓取

[英]Web scraping using BeautifulSoup

我正在嘗試從黃頁中抓取數據,網站就是這樣

我想要這個div class= search-results listing-group

我試過了

parent = soup.find('div',{'class':"search-results listing-group"})

但是,我沒有任何結果。

該URL具有適當的防刮擦保護,可以阻止以編程方式提取HTML。 這就是為什么您沒有得到任何輸出的主要原因。 您可以通過檢查從請求返回的原始數據來查看此信息:

from bs4 import BeautifulSoup
import requests
url = "https://www.yellowpages.com.au/find/boat-yacht-sales/melbourne-vic"
soup = BeautifulSoup(requests.get(url).text)

print(soup)

摘抄:

當在線數據保護服務檢測到來自您的計算機網絡的請求似乎違反了我們網站的使用條款時,將顯示此頁面。

您在使用請求嗎? 網頁似乎不允許自動抓取,至少要使用Beautiful Soup。 我嘗試為您抓取它,這就是我在內容中看到的內容。

 <p style="font-weight: bold;">Why did this happen?</p> <p style="margin-top: 20px;">This page appears when online data protection services detect requests coming from your computer network which appear to be in violation of our website's terms of use.</p> </div>, <div style="border-bottom: 1px #E7E7E7 solid; margin-top: 20px; margin-bottom: 20px; height: 1px; width: 100%;"> </div>, <div style="margin-left: auto; margin-right: auto; font-size: 20px; max-width: 460px; text-align: center;"> We value the quality of content provided to our customers, and to maintain this, we would like to ensure real humans are accessing our information.</div>, <div style="margin-left: auto; margin-right: auto; margin-top: 30px; max-width: 305px;"> 

您可能必須嘗試其他(合法)刮取方法。

您正在訪問的頁面似乎不允許靜態抓取,您需要像這樣使用使用硒的高級抓取。

from bs4 import BeautifulSoup
import requests
from selenium import webdriver

url = "https://www.yellowpages.com.au/find/boat-yacht-sales/melbourne-vic"

driver=webdriver.Chrome(executable_path="{location}/chromedriver")
driver.get(url)
content_element = driver.find_elements_by_xpath("//div[@class='search-results 
listing-group']")
content_html = content_element[0].get_attribute("innerHTML")
soup = BeautifulSoup(content_html, "html.parser")
print soup

由於類名包含space,因此您需要使用xpath或id之類的東西來獲取數據。 有關高級抓取的更多信息,請閱讀以下內容: https : //medium.com/dualcores-studio/advanced-web-scraping-in-python-d19dfccba235

暫無
暫無

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

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