首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用BeautifulSoup进行网络抓取

利用BeautifulSoup进行网络抓取
EN

Stack Overflow用户
提问于 2017-05-14 09:09:22
回答 3查看 397关注 0票数 0

我试着从黄页上抓取数据,网站是

我想要这个div class= search-results listing-group

我试过这个

代码语言:javascript
复制
parent = soup.find('div',{'class':"search-results listing-group"})

但是,我没有得到任何结果。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-05-14 09:41:55

此URL具有防刮保护,可抵抗编程HTML提取.这就是你没有得到任何输出的主要原因。通过检查从请求返回的原始数据,您可以看到这一点:

代码语言:javascript
复制
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)

节选:

当在线数据保护服务检测到来自您的计算机网络的请求时,出现此页面,该请求似乎违反了我们网站的使用条款。

票数 0
EN

Stack Overflow用户

发布于 2017-05-14 09:42:07

你在使用请求吗?该网页似乎不允许自动抓取,至少使用美汤。我试过帮你刮,这就是我在内容中看到的。

代码语言:javascript
复制
<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;">

您可能需要尝试其他(合法的)方法来抓取它。

票数 0
EN

Stack Overflow用户

发布于 2017-05-14 09:50:22

您正在访问的页面似乎不允许静态抓取,您需要像这样使用selenium预先抓取。

代码语言:javascript
复制
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

因为类名包含空间,所以需要使用xpath或id之类的东西来获取数据。欲了解更多关于预先抓取的信息,请阅读以下内容:https://medium.com/dualcores-studio/advanced-web-scraping-in-python-d19dfccba235

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43962202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档