我试着练习使用BeautifulSoup
。我正试图从这个网站上提取足球运动员的图片地址:https://www.transfermarkt.com/jordon-ibe/profil/spieler/195652
当我“inspect
”代码时,包含img
src
的部分如下:
<div class="dataBild">
<img src="https://tmssl.akamaized.net//images/portrait/header/195652-1456301478.jpg?lm=1456301501" title="Jordon Ibe" alt="Jordon Ibe" class="">
<div class="bildquelle"><span title="imago">imago</span></div>
</div>
所以我想我可以用BeautifulSoup
来找到div
和{
这会运行,但不会输出任何内容。所以我只需运行print(soup)
# Import the Libraries that I need
import urllib3
import certifi
from bs4 import BeautifulSoup
# Specify the URL
url = 'https://www.transfermarkt.com/jordon-ibe/profil/spieler/195652'
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
response = http.request('GET', url)
#Parse the html using beautiful soup and store in variable 'soup'
soup = BeautifulSoup(response.data, "html.parser")
print(soup)
这个输出
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr/><center>nginx</center>
</body>
</html>
所以它显然没有从网页上提取所有的HTML?
为什么会这样?我寻找div class = DataBild sound
的逻辑是什么?在
站点似乎在检查请求的
User-Agent
头是否有效。在所以你需要像这样添加标题:
这将打印
200
。如果删除标题,则得到404
。在任何非空的
User-Agent
值(在修剪空白之后)似乎都可以工作。在相关问题 更多 >
编程相关推荐