【问题标题】:Python Request entire HTML page, instead of initially loaded contentPython 请求整个 HTML 页面,而不是最初加载的内容
【发布时间】:2020-09-30 08:58:14
【问题描述】:

我正在尝试在 PlayStore 上公开获得一些评论数据,并且由于提供的 API 只允许获取自己的应用程序的评论,我正在尝试从网络上抓取它。

我正在使用 requests 包来获取 PlayStore 上给定应用程序的 HTML 页面,并将使用 BeautifulSoup 对其进行解析并将其保存到文件中,然后提取相关内容(每个用户的评分和评论)。

我的问题是使用request.get(URL) 检索的不是页面的全部内容。 导航到 PlayStore 上应用程序的“阅读所有评论”,您会进入包含该应用程序所有评论的页面。不幸的是,第一次加载页面时只有有限的评论集加载,而其余评论仅在向下滚动到底部时加载。通过调用 request.get(URL) 仅检索有限的评论集,而不是所有评论。

尝试导航到https://play.google.com/store/apps/details?id=com.bendingspoons.thirtydayfitness&hl=en&showAllReviews=true,只有在滚动到页面底部时才会加载较早的评论。

有没有办法访问整个页面/触发加载更多评论/模拟滚动?

下面是我的代码:

# get reviews for Thirty Days of Fitness app
URL = "https://play.google.com/store/apps/details?id=com.bendingspoons.thirtydayfitness&hl=en&showAllReviews=true"

# make request
request = requests.get(URL)
# extract HTML text
raw_text = request.text

# parse HTML and prettify
soup = BeautifulSoup(raw_text, 'html.parser')
text = soup.prettify()

# write to file
save_path = './thirtydayfitness_html.txt'
with open(save_path, 'w+', encoding=request.encoding) as f:
    f.write(text)

【问题讨论】:

    标签: python html python-3.x web web-scraping


    【解决方案1】:

    会考虑使用网络驱动程序向下滚动。像这样

    SCROLL_PAUSE_TIME = 0.5
    
    # Get scroll height
    last_height = driver.execute_script("return document.body.scrollHeight")
    
    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    
        # Wait to load page
        time.sleep(SCROLL_PAUSE_TIME)
    
        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == last_height:
            break
        last_height = new_height
    

    参考:-How can I scroll a web page using selenium webdriver in python?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多