【发布时间】:2022-01-11 01:24:14
【问题描述】:
我想使用BeautifulSoup 从Google Arts & Culture 检索信息。
我检查了许多 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']
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
【问题讨论】:
-
你检查过
html.text变量中相同元素的xpath是什么?
标签: python beautifulsoup web-crawler