您正在尝试从 div 元素内部检索所有链接:
links1 = [a['href'] for a in soup.find("div", {"class": "page-inner"}).find_all('a', href=True)]
查看该元素的内容。如果我们运行以下
代码:
div = soup.find("div", {"class": "page-inner"})
print(div)
我们看到了:
<div class="page-inner">
<header data-v-0225089c="">
<a class="logo nuxt-link-active" data-v-0225089c="" href="/"
title="Hostelworld">
<img alt="Hostelworld" data-v-0225089c=""
src="/_nuxt/img/789e4da.svg" />
</a>
<div class="header-icons" data-v-0225089c="">
<div class="user-authentication item" data-v-0225089c="">
<button aria-label="Se connecter/créer un compte"
class="header-option" data-v-0225089c="" id="header-login"
title="Se connecter/créer un compte">
<i aria-describedby="icon-core-user-fill-aria-description"
aria-hidden="true"
aria-labelledby="icon-core-user-fill-aria-label"
class="core-icon icon-core-user-fill" data-v-0225089c="">
<!-- -->
<!-- -->
</i>
<span class="sr-only" data-v-0225089c="">Se
connecter/créer un compte</span>
</button>
<!-- -->
<!-- -->
</div>
<div class="select-list item" data-v-0225089c=""
data-v-5ec8cd6c="" id="header-language-picker"
role="listbox">
<div class="select-list-slot-wrapper" data-v-5ec8cd6c=""
tabindex="0">
<div class="icon-menu-item" data-v-0225089c=""
data-v-5ec8cd6c="" role="option"
title="Français">Français</div>
</div>
<!-- -->
</div>
<div class="select-list item" data-v-0225089c=""
data-v-5ec8cd6c="" id="header-currency-picker"
role="listbox">
<div class="select-list-slot-wrapper" data-v-5ec8cd6c=""
tabindex="0">
<div class="icon-menu-item" data-v-0225089c=""
data-v-5ec8cd6c="" role="option" title="USD">USD</div>
</div>
<!-- -->
</div>
<button aria-label="Menu"
class="icon-core-menu-fill header-option item"
data-v-0225089c="" title="Menu">
<span class="sr-only" data-v-0225089c="">Menu</span>
</button>
</div>
<!-- -->
<!-- -->
</header>
</div>
请注意,除了这个之外,其中没有 <a> 元素:
<a class="logo nuxt-link-active" data-v-0225089c="" href="/"
title="Hostelworld">
<img alt="Hostelworld" data-v-0225089c=""
src="/_nuxt/img/789e4da.svg" />
</a>
这意味着您的代码工作正常。问题是,
您在浏览器中看到的内容是在加载后通过 Javascript 加载的
初始 HTML 内容。如果您使用开发人员,您可以看到这一点
浏览器中的工具来查看由
加载页面。对于您给出的示例,我们看到一个请求
JSON 文档如下所示:
GET https://api.m.hostelworld.com/2.2/cities/14/properties/?currency=USD&application=web&user-id=ae71369c-e75b-45e5-8ffa-97223f9daf22&date-start=2021-04-30&num-nights=3&guests=2&per-page=1000&show-rooms=1&property-num-images=30
如果您请求该 URL,您将获得一个 JSON 文档,其中可能包含所有
你想要的信息:
>>> import requests
>>> url='https://api.m.hostelworld.com/2.2/cities/14/properties/?currency=USD&application=web&user-id=ae71369c-e75b-45e5-8ffa-97223f9daf22&date-start=2021-04-30&num-nights=3&guests=2&per-page=1000&show-rooms=1&property-num-images=30'
>>> res=requests.get(url)
>>> print('\n'.join(property['name'] for property in res.json()['properties']))
Enjoy Hostel
Peace & Love Hostel
Generator Paris
Le Village Montmartre by Hiphophostels
Aloha Eiffel Tower by Hiphophostels
MEININGER Paris Porte de Vincennes
Smart Place Paris Gare du Nord by Hiphophostels
.
.
.