Python BeautifulSoup 发现所有返回空的 div

Python BeatifulSoup find all divs returning empty

我正在尝试从葡萄牙房地产代理网站获取所有房屋清单。我正在使用以下一小段代码:

import requests
import bs4
import os

REAL_ESTATE_AGENCY_URL= os.getenv('REAL_ESTATE_AGENCY_URL')

response = requests.get(REAL_ESTATE_AGENCY_URL)
response.raise_for_status()
content = response.text

soup = bs4.BeautifulSoup(content, 'lxml')

all_listings = soup.find_all(name="div", class_="listing-card")
print(all_listings)

我正在使用的 link 是:this one(纯文本太长了)。 print 语句只打印一个空数组。如下图所示,包含所有信息的 div 的 class 为 listing-card。我不明白我做错了什么。

您正在返回空结果,因为该网站完全依赖于 JavaScript.BeautifulSoup 无法模仿数据,但您可以仅使用 [=] 从 api 调用 json 响应中轻松获取数据12=] module.Here 是工作示例。

脚本:

import requests
import json
body= {"filters":[{"field":"BusinessTypeID","value":"1","type":0},{"field":"NumberOfBedrooms","biggerThan":2,"type":4},{"field":"ListingTypeID","shouldValues":["11","1"],"type":2},{"field":"Region1ID","value":"78","type":0}],"sort":{"fieldToSort":"ContractDate","order":1}}
headers= {
    'content-type': 'application/json',
   }

api_url = "https://www.remax.pt/Api/Listing/MultiMatchSearch?page=1&searchValue=&size=20"


jsonData = requests.post(api_url, data=json.dumps(body), headers=headers).json()
#print(jsonData)
for item in jsonData['results']:
    price=item['listingPriceText']
    print(price)

输出:

125 000 €
137 500 €
132 500 €
142 500 €
132 500 €
152 500 €
132 500 €
142 500 €
135 000 €
145 000 €
110 000 €
198 000 €
120 000 €
260 000 €
265 000 €
305 000 €
252 500 €
500 000 €
75 000 €
142 000 €