无法使用Python请求读取HTML页面
我有一个包含图像的简单页面。我想在本地保存图像。因此,我使用无法使用Python请求读取HTML页面,python,html,beautifulsoup,python-requests,Python,Html,Beautifulsoup,Python Requests,我有一个包含图像的简单页面。我想在本地保存图像。因此,我使用beautfulsoup来刮取src。以下是我的代码: def getImage(url): page = requests.get(url).text #print(page) soup = BeautifulSoup(page, 'lxml') #print(soup) img = soup.find(name='img')
beautfulsoup
来刮取src
。以下是我的代码:
def getImage(url):
page = requests.get(url).text
#print(page)
soup = BeautifulSoup(page, 'lxml')
#print(soup)
img = soup.find(name='img')
if img is not None:
#img = img.get('src')
print(img.attrs)
如果我打印页面
,这是我将得到的。
我还检查了页面是否为HTML,它显示它不是。但我不知道这种情况下还有什么其他类型。
我还尝试使用不同的解析器,比如lxml
和html5lib
这是我直接复制的HTML页面:
<html><head><meta name="viewport" content="width=device-width, minimum-scale=0.1">
<title>SOMETHING TITLE</title>
</head>
<body style="margin: 0px; background: #0e0e0e;">
<img style="-webkit-user-select: none;margin: auto;cursor: zoom-in;" src="http:<WHATEVER>" width="500" height="279">
</body></html>
标题
“width=“500”height=“279”>
页面是否加密?这应该是一个简单的抓取:(html中没有名为“img”的属性。您可以添加一个名称(即 但是,如果您无法更改HTML,可以执行以下操作:
images = soup.findAll('img')
for image in images:
# do whatever
问题是在代码到达bs4部分之前,
请求
未能将其作为HTML读取。相反,它给出了不同的未知格式,我不知道是什么。请参阅链接附件。我无法编辑HTML。请尝试以下提示:我找到了问题的原因。页面内容类型为图像/png
。因此我不需要d使用bs4
走大路。相反,我可以直接从requests
本地下载图像。谢谢!
img = soup.find(name='myImage')
images = soup.findAll('img')
for image in images:
# do whatever