Beautiful Soup select google 图片returns 空列表

Beautiful Soup select google image returns empty list

我想使用 BeautifulSoupGoogle Arts & Culture 检索信息。 我检查了很多 Whosebug 帖子 ([1], [2], , [4], ), 仍然检索不到信息。

我想要每个图块(图片)的(li)信息,例如 href,但是,find_allselect one return 空列表或 None.

你能帮我得到下面锚标签的 href 值 class "e0WtYb HpzMff PJLMUc" 吗?

href="/entity/claude-monet/m01xnj?categoryId=artist"

下面是我试过的。

import requests
from bs4 import BeautifulSoup

url = 'https://artsandculture.google.com/category/artist?tab=time&date=1850'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
print(soup.find_all('li', class_='DuHQbc'))                 # []
print(soup.find_all('a', class_='PJLMUc'))                  # []
print(soup.find_all('a', class_='e0WtYb HpzMff PJLMUc'))    # []
print(soup.select_one('#tab_time > div > div:nth-child(2) > div > ul > li:nth-child(2) > a'))  # None
for elem in soup.find_all('a', class_=['e0WtYb', 'HpzMff', 'PJLMUc'], href=True):
    print(elem)  # others with class 'e0WtYb'

...
# and then something like elem['href']

https://artsandculture.google.com/category/artist?tab=time&date=1850

从 Chrome

复制选择器

#tab_time > div > div:nth-child(2) > div > ul > li:nth-child(2) > a

不幸的是,问题不在于您使用 BeautifulSoup 错误。您请求的网页似乎缺少内容!我将 html.text 保存到文件以供检查:

为什么会这样? 因为网页实际上使用 JavaScript 加载其内容。当您在浏览器中打开网站时,浏览器会执行 JavaScript,这会将所有艺术家方块添加到网页中。 (你甚至可能会注意到当你第一次加载网站时方块不存在的短暂时刻。)另一方面,requests 不执行 JavaScript——它只是下载网页并将它们保存到字符串中。

你能做些什么?不幸的是,这意味着抓取网站将非常困难。在这种情况下,我建议寻找其他信息来源或使用网站提供的 API。