【问题标题】:BeautifulSoup empty array as resultBeautifulSoup 空数组作为结果
【发布时间】:2020-06-17 22:40:53
【问题描述】:

我是 beautifulSoup 的新手,我想知道为什么当我尝试检索 this 网站的船只价格时,它返回一个空数组?

这是我的代码:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.moorings.co.uk/destinations/americas/brazil/paraty-sailing-holidays#!date=2020-8-1&departureId=5422461&yachtId=91&price=3444&sortOrder=Price%20low%20to%20high&showAllDepartures=false&hullType=All&length=All&yachtClass=All&numberOfCabins=All&numberOfDays=7&numberOfPassengers=2&numberOfToilets=All&numberOfShowers=All&productType=1')
page_soup = BeautifulSoup(source.text, 'html.parser')
pages = page_soup.find_all("h4", {"class":"text-align-left"})

print(len(pages))
print(pages)

它返回一个空数组,所以长度 = 0

请告诉我我做错了什么......

如果不能,是否可以使用网站的python检索Crtl-Shift-I的文本??

【问题讨论】:

  • 看起来网站没有立即加载船价,实际上加载速度也非常慢。您不能在内容动态加载的网站上使用 beautifulsoup,请查看使用 selenium 或类似库
  • 显示你想要的输出会更容易回答。
  • 好的,谢谢,除了 selenium,你还有什么推荐的?
  • @HumayunAhmadRajib 我想要的输出是在页面上有一个包含船只价格的数组

标签: python beautifulsoup python-requests


【解决方案1】:

由于内容是动态加载的,您不能直接使用 beautifulsoup。 您必须首先解释 html/javascript,这会导致使用有关船的实际信息更新 dom。

例如,您可以使用硒。

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Chrome("C:\\Users\\kuehn\\Downloads\\chromedriver_win32\\chromedriver")
driver.get('https://www.moorings.co.uk/destinations/americas/brazil/paraty-sailing-holidays#!date=2020-8-1&departureId=5422461&yachtId=91&price=3444&sortOrder=Price%20low%20to%20high&showAllDepartures=false&hullType=All&length=All&yachtClass=All&numberOfCabins=All&numberOfDays=7&numberOfPassengers=2&numberOfToilets=All&numberOfShowers=All&productType=1')
html = driver.page_source
soup = BeautifulSoup(html, features='lxml')

for tag in soup.find_all("h4", {"class":"text-align-left"}):
    print(tag.text)

结果对我来说是

PS C:\stackoverflow\beautiful_soup> python .\main.py

DevTools 监听 ws://127.0.0.1:53420/devtools/browser/d86a7c8b-d960-4b45-8849-2d09b7bbebda

Moorings 413 - 3 舱单体船

Moorings 419 - 3 舱单体船

Moorings 45 - 3 舱单体船

Moorings 5​​1.4 - 4 舱单体船

Moorings 413 - 3 Cabin Monohulledit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-05
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2019-02-19
    相关资源
    最近更新 更多