Jih*_*hoi 5 python beautifulsoup web-crawler
我想使用从Google Arts & CultureBeautifulSoup检索信息。我检查了许多 stackoverflow 帖子(、、、、、
)
[1],
但
仍然无法检索到信息。[2][3][4][5]
但是,我想要每个图块(图片)的 ( li) 信息,例如 href,find_all并select one返回空列表或无。
您能帮我获取“e0WtYb HpzMff PJLMUc”类锚标记的以下 href 值吗?
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']
Run Code Online (Sandbox Code Playgroud)
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\xe2\x80\x94,它只是下载网页内容并保存它们到一个字符串。
你能为这个做什么?不幸的是,这意味着抓取网站将非常困难。在这种情况下,我建议寻找替代信息来源或使用网站提供的 API。
\n