用靓汤清理宠物收养网站

2024-10-02 22:33:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难在这个网站上找到每只宠物的divhttps://indyhumane.org/adoptable-cats/同时尝试使用python和Beautiful Soup来获取细节

当我检查页面并检查html源代码时,我看到包含每个pet配置文件的div带有一个class = "mbcpp_result_animal",但是当我使用下面的代码时,containers的长度为零

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://indyhumane.org/adoptable-cats/'

uClient = uReq(my_url)

page_html = uClient.read()

uClient.close()

page_soup = soup(page_html, "html.parser")

containers = page_soup.findAll("div",{"class":"mbcpp_result_animal"})

print(len(containers))

如果我打印page_soup.body,我看不到任何带有class="mbcpp_result_animal"的div,这与我在chrome的开发工具中检查页面时的html源代码不同

这是我第一次刮网。所以,我觉得我还没有完全理解这个过程。有人能告诉我需要做什么来解决这个问题吗


Tags: httpsorgdivhtmlpageresultclasssoup
1条回答
网友
1楼 · 发布于 2024-10-02 22:33:23

因为这来自页面为检索结果和更新页面而进行的附加API调用。该调用具有querystring参数,您可以看到如下所示

它包括API端点、一个API键、限制搜索的条件和一个最终的随机数参数,可能是为了避免缓存结果。你可以引入一个实际的随机数

import requests

r = requests.get('https://indyhumane.org/wp-content/plugins/mbc-petpoint/mbc-petpoint-data.php?species_name=cats&sex=A&ageGroup=All&orderBy=ID&primaryBreed=All&secondaryBreed=All&specialNeeds=A&api_key=14j14u8qzj27aqw6tv53k553lxcjff0xf2uh16i61t4s61g727&num_items=-1&apimode=AdoptableSearch&location=&rnd=1')
r.json()

相关问题 更多 >