作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 Python 创建了一个脚本,只从网页上抓取食品店的名称。但是,当我执行我的脚本时,出现以下错误。
name = soup.select_one("h1.listing-name").text
AttributeError: 'NoneType' object has no attribute 'text'
到目前为止我的尝试:
from bs4 import BeautifulSoup
import requests
url = "https://www.yellowpages.com.au/sa/gawler/mega-health-gawler-14366108-listing.html"
with requests.Session() as s:
s.headers["User-Agent"] = "Mozilla/5.0"
response = s.get(url)
soup = BeautifulSoup(response.text,"lxml")
name = soup.select_one("h1.listing-name").text
print(name)
我想要的内容不是动态生成的。此外,我在脚本中使用的选择器是完美无缺的。如何从该站点打印该商店的名称?
最佳答案
我已经修改了您的脚本以查看它从服务器获取的内容:
from bs4 import BeautifulSoup import requests
url = "https://www.yellowpages.com.au/sa/gawler/mega-health-gawler-14366108-listing.html"
with requests.Session() as s:
s.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
response = s.get(url)
soup = BeautifulSoup(response.text,"lxml")
if soup is not None:
selected = soup.select_one("h1.listing-name")
if selected is not None:
name = soup.selected.text
print(name)
else:
print("Oh No!\n{}".format(soup))
else:
print("Ooops!\n{}".format(response))
然后我运行了它。结果是下面的验证码页面。您需要弄清楚如何绕过验证码,否则您的脚本将看不到内容,因此无法抓取它。
Oh No!
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<title>Yellow Pages® | Data Protection</title>
<link href="/favicon.ico?v=2" rel="shortcut icon"/>
<!--[if (lt IE 9)&!(IEMobile)]><script src="/assets/ie/respond.sensis-9575467dfbc008e5b0d486dc4f481624.js" type="text/javascript" ></script><![endif]-->
<!--[if (lt IE 10)&!(IEMobile)]><script src="/assets/ie/custom-event-ie9.js" type="text/javascript"
></script><![endif]-->
<!--[if (lt IE 10)&!(IEMobile)]><link rel="stylesheet" href="/assets/ie/gradient-hacks-ie89-12453d23f1fec3d9d46e56cc6e023576.css"/><![endif]-->
<script async="" defer="" src="https://www.google.com/recaptcha/api.js?"></script>
<meta content="NOINDEX, NOFOLLOW" name="ROBOTS"/>
</head>
<body id="" style="border-width: 0;
background-color: #EDEDED;
font-size: 85%;
line-height: 1.3;
margin: 0;
font-family: Helvetica, sans-serif;">
<div style="padding: 10px 15px;
height: 70px;
min-height: 45px;
background-color: #ffce00;
background-image: linear-gradient(to right, #ffce00, #fedb55, #ffce00);
box-shadow: inset 0px -5px 7px -5px rgba(0, 0, 0, 0.35);">
<div style="position: relative;
max-width: 1240px;
margin: 0 auto;">
<a href="/">
<img alt="Yellow Pages" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAACMCAYAAACuwEE+AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAFa5JREFUeNrsXQl4FMXWrYQlLLIJAgYEBAFZZAeRIKs8FgU1GkBQCPIEcQH9hfwqAoryFEVx+9gVAYOCCgq4REXBCIYlbD4gQESW4BKRhE2IIel3T0/N2Pv0TCaTnpk633cI011d3VV1+9atW7eqo1hkIYZYk/MKYjViJeJl/G8UsaLmmvNEiXiWeI7/zSH+QczmzIuUCowK47LVIHYhdiReR2xNbFBM9zpC3E38kbiNuJl4kgk4GpWJ8cSFxAyuFUqSGfxZ4vmzCTgAscSHiRuJ+Q4QEjPm82d8mD+zQBBRgZhIXE8sdLCQmLGQP3siL4tAMaEFcQ4xNwSFxIy5vEwtRPMGDr2Jn4aRkJjxU15WAT/Rh5gWAYKiZRovu4BNdCZuiEBB0XIDrwsBE9Q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RuwQn6TaMSgcROv85AGPmS4UjRmsXMlr+uwACY5pxALRMMGnAW8bqNYGAILvE+JRg4YTzEfF82HIuqzIO5yFcZM5XUZEShFnMz4V74EfWI+r7tSLALRlpguhMA203mdRTQwg4pvEZ8XAmHK87yOSjMBD+oQk4Vw6JjM60bABJ2JG4SgyHXQWYiDfWA1XloECkoaK8aViJGA3sy1G3W4C8qnvKwCAQL2M0VMam4YCUkuL1ML0bzFB+zEnEhcTywMQSEp5M+eyMsiEERgz/SHuYHoZCdgPn/Gh/kzCzgACHLG7uULiRkOEJIM/izxzIEB2P4iKowFCLsS4NNzHYn4+kRrYoNiutcR5orMxz7124ibmesrvWGHqAjTQjHEmpwINMKnQbAnbSVO1EdFzTVuD/RZTuw0mcNcW2Vkc+ZFSgVG4ZMtAgJ28T8BBgAcyn1tKfpknwAAAABJRU5ErkJggg==" style="width: 70px;"/>
</a>
</div>
</div>
<div style="padding-top: 10px;">
<div style="margin-left: auto;
margin-right: auto;
max-width: 600px;
vertical-align: top;">
<div style="background-color: #FFFFFF;
border-radius: 8px;
padding: 1px 10px;">
<h1 style="font-weight: normal;">We have detected unusual traffic activity originating from your IP address.</h1>
<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;">
<form action="/dataprotection" method="post" name="captcha" style="margin: 0; padding: 0; word-wrap: break-word; display: block;">
<div class="g-recaptcha" data-sitekey="6LeukxwTAAAAANIgmFm7-cOKIY4avRNHiDB9xAD8"></div>
<noscript>
<div style="width: 302px; height: 352px;">
<div style="width: 302px; height: 352px; position: relative;">
<div style="width: 302px; height: 352px; position: absolute;">
<iframe frameborder="0" scrolling="no" src="https://www.google.com/recaptcha/api/fallback?k=6LeukxwTAAAAANIgmFm7-cOKIY4avRNHiDB9xAD8" style="width: 302px; height:352px; border-style: none;">
</iframe>
</div>
<div style="width: 250px; height: 80px; position: absolute; border-style: none;
bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">
<textarea class="g-recaptcha-response" id="g-recaptcha-response" name="g-recaptcha-response" style="width: 250px; height: 80px; border: 1px solid #c1c1c1;
margin: 0px; padding: 0px; resize: none;" value="">
</textarea>
</div>
</div>
</div>
</noscript>
<input name="path" type="hidden" value="/sa/gawler/mega-health-gawler-14366108-listing.html"/>
<div style="margin-left: auto;
margin-right: auto;
text-align: center;
padding: 15px 0;
max-width: 260px;
margin-top: 30px;">
<button class="submit" style="width: 100%;
color: black;
padding: 10px 25px;
border-radius: 25px;
cursor: pointer;
border: none;
position: relative;
background-color: #ffce00;
display: inline-block;
text-align: center;
box-sizing: border-box;">Submit</button>
</div>
</form>
</div>
<div style="border-bottom: 1px #E7E7E7 solid;
margin-top: 20px;
margin-bottom: 20px;
height: 1px;
width: 100%;"></div>
<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>
</div>
</body>
</html>
We have detected unusual traffic activity originating from your IP address. 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.
我想合乎道德的做法是与网页管理员一起工作,或者至少征求许可。
关于python - 我的脚本无法从网页获取食品店的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53269763/
我是一名优秀的程序员,十分优秀!