【问题标题】:Very basic web scraping query for you fine folk [closed]非常基本的网络抓取查询,适合您的好人[关闭]
【发布时间】:2020-04-14 19:23:57
【问题描述】:

只是在做一个小项目,我想通过网络获取特定商品的价格。我以前从未这样做过,我在使用一些方法时遇到了困难。

这是有问题的网页: https://www.made.com/keira-office-chair-cloud-grey-and-copper

这是有问题的元素:

<div class="ProductPrice__PriceWrapper-rtg8id-0 guIZZV"><span class="ProductPrice__Price-rtg8id-1 jYfukd">£149</span><div></div><div></div></div>    <span class="ProductPrice__Price-rtg8id-1 jYfukd">£149</span><div></div><div></div>

我正在尝试的代码如下:

import requests, bs4

URL = 'https://www.made.com/keira-office-chair-cloud-grey-and-copper'
page = requests.get(URL,headers={"User-Agent":"Defined"})
page.raise_for_status()
soup = bs4.BeautifulSoup(page.content, 'html.parser')
price = soup.find('ProductPrice__Price-rtg8id-1 jYfukd').get_text()
print(price)

我收到的错误是: ttributeError: 'NoneType' object has no attribute 'get_text'

我环顾四周,似乎一种更简单的方法是使用 CSS 选择器,但是我已经尝试过(可能不正确),当我运行我的脚本时,它只返回“[]”。

谁能解释我做错了什么,或者我应该寻找什么?

【问题讨论】:

  • 如果我没记错的话,您没有在页面上运行任何 javascript,因此动态加载的内容根本不会出现在您的请求中。您将需要使用像selenium 这样实际呈现页面的东西。 here 是我刚刚发现的这类东西的教程

标签: python html css web-scraping


【解决方案1】:


page = requests.get('https://www.made.com/keira-office-chair-cloud-grey-and-copper',headers={"User-Agent":"Defined"})
soup = bs4.BeautifulSoup(page.text, 'html.parser')
price = soup.find('span', attrs={"class":"ProductPrice__Price-rtg8id-1"}).get_text()
print(price)

如果要检查是否动态加载,只需按 CTRL + U 检查页面源和 CTRL + F 为您要查找的字符串。这与 python 请求获取的数据相同。

这个网站会有点难以抓取,因为它有Distil bot blocking.,不过我真的没有绕过它的经验。 如果您没有被该站点阻止,我发布的 POC 应该可以工作。您可以通过打印出汤/来源并搜索distilIdentificationBlock div 来检查这一点。

【讨论】:

  • 谢谢,确实被屏蔽了!我尝试了另一个网站(Tesco.co.uk)并设法将价格刮掉了。干杯!
【解决方案2】:

问题在于,从文档中您发现没有属性“ProductPrice__Price-rtg8id-1 jYfukd” 相反,为了让您查看实际得到的内容,您可以打印收到的文档的输出,然后从那里开始抓取。

以下是您收到的文档的示例和有效获取:

import requests
import bs4
url =  "https://www.made.com/keira-office-chair-cloud-grey-and-copper"
page = requests.get(url,headers={"User-Agent":"Defined"})
page.raise_for_status()
soup = bs4.BeautifulSoup(page.content, 'html.parser')
price = soup.find(id="distilIdentificationBlock")
print(price)
print(soup)

这是输出:

<div id="distilIdentificationBlock"> </div> <!--output of the soup.find-->

下面的代码就是你得到的html文档:

<!DOCTYPE html>

<html>
<head>
<meta content="NOINDEX, NOFOLLOW" name="ROBOTS"/>
<meta content="max-age=0" http-equiv="cache-control">
<meta content="no-cache" http-equiv="cache-control"/>
<meta content="0" http-equiv="expires"/>
<meta content="Tue, 01 Jan 1980 1:00:00 GMT" http-equiv="expires"/>
<meta content="no-cache" http-equiv="pragma"/>
<meta content="10; url=/distil_r_captcha.html?requestId=996b9d94-b538-4e4f-9afe-67434911be37&amp;httpReferrer=%2Fkeira-office-chair-cloud-grey-and-copper" http-equiv="refresh"/>
<script type="text/javascript">
        (function(window){
                try {
                        if (typeof sessionStorage !== 'undefined'){
                                sessionStorage.setItem('distil_referrer', document.referrer);
                        }
                } catch (e){}
        })(window);
</script>
<script defer="" src="/jyqoxgnrwlzilgpz.js" type="text/javascript"></script><style type="text/css">#d__fFH{position:absolute;top:-5000px;left:-5000px}#d__fF{font-family:serif;font-size:200px;visibility:hidden}#szabqxaftfdvevsqbsbcubrbzcuffvaw{display:none!important}</style></meta></head>
<body>
<div id="distilIdentificationBlock"> </div>
</body>
</html>

【讨论】:

  • 非常感谢您的帮助!看起来该网站正在使用机器人拦截器......因为这只是一个概念证明,我将决定在另一个网站上进行练习。谢谢!
  • 很高兴为您提供帮助! :D
猜你喜欢
  • 2018-04-17
  • 2013-11-16
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多