You need to enable JavaScript to run this app. 如何使用BeautifulSoup抓取landwatch.com中Read More后的隐藏内容
优惠活动
大模型
产品
解决方案
定价
更多

如何使用BeautifulSoup抓取landwatch.com中Read More后的隐藏内容

问题背景
  • 近期在筹备购置土地,计划开发小工具把相关信息同步到Jira/Confluence中,方便单独记录每块土地的对接人员及沟通细节
  • 已编写面向landwatch.com的爬虫,当前可正常运行,但d19de类对应的大量内容被隐藏在Read More按钮后,无法完整抓取
  • 不希望使用Selenium,仅希望通过BeautifulSoup实现绕过Read More按钮抓取完整内容的需求
现有爬虫代码
from bs4 import BeautifulSoup
import requests


def get_property_data(url):
    headers = ({'User-Agent':
                    'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'})
    response = requests.get(url, headers=headers)  # Maybe request Url with read more already gone
    soup = BeautifulSoup(response.text, 'html5lib')
    title = soup.find_all(class_='b442a')[0].text
    details = soup.find_all('p', class_='d19de')
    price = soup.find_all('div', class_='_260f0')[0].text
    deets = []
    for i in range(len(details)):
        if details[i].text != '':
            deets.append(details[i].text)
    detail = ''
    for i in deets:
        detail += '<p>' + i + '</p>'
    return [title, detail, price]

注:上述代码中url为网站上的单个地块列表页,测试用URL为https://www.landwatch.com/huerfano-county-colorado-recreational-property-for-sale/pid/410454403

已尝试的解决方案

之前找到的示例代码无法复用或已失效,对应代码如下:

import requests ; from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get("http://www.mouthshut.com/product-reviews/Lakeside-Chalet-Mumbai-reviews-925017044").text, "html.parser")
for title in soup.select("a[id^=ctl00_ctl00_ContentPlaceHolderFooter_ContentPlaceHolderBody_rptreviews_]"):
    items = title.get('href')
    if items:
        broth = BeautifulSoup(requests.get(items).text, "html.parser")
        for item in broth.select("div.user-review p.lnhgt"):
            print(item.text)
可行解决方案

经过对目标站点结构的验证,landwatch的完整地块描述内容默认就存在于页面源码的script标签中,不需要额外触发Read More按钮,只需要解析对应JSON数据即可,调整后的代码如下:

from bs4 import BeautifulSoup
import requests
import json

def get_property_data(url):
    headers = ({'User-Agent':
                    'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'})
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html5lib')
    # 提取页面预加载的完整数据
    raw_data = soup.find("script", id="__NEXT_DATA__").text
    json_data = json.loads(raw_data)
    property_info = json_data["props"]["pageProps"]["property"]
    # 直接取完整字段,无需拼接零散p标签
    title = property_info["listingTitle"]
    price = property_info["price"]
    full_detail = property_info["description"]
    # 可以直接按需求转成p标签格式
    detail = ''.join([f"<p>{line}</p>" for line in full_detail.split('\n') if line.strip()])
    return [title, detail, price]

方案说明

  • 目标站点是Next.js构建的单页应用,所有页面数据会预先注入到id="__NEXT_DATA__"的script标签中,Read More只是前端的展示控制逻辑,完整内容本身就存在于首次请求的返回里
  • 无需额外请求接口、无需模拟点击,直接解析JSON字段就能拿到全部描述内容,比抓取前端渲染的标签更稳定,不受前端类名变更影响

内容的提问来源于stack exchange,提问作者Pyrometheous

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 07:24:05