首页 > 解决方案 > 为什么我无法通过 Python 使用 requests_html 获取整个页面的内容?

问题描述

我试图用 Python从这个网站https://aptekazawiszy.pl/获取数据。我必须使用他们的搜索引擎搜索产品,如果该产品存在,请提取指向它的直接链接,向该站点发出请求,然后获取它的一些数据。问题是,我没有收到搜索结果页面的完整 HTML。我正在使用 urllib3 请求和 BeautifulSoup4。我认为该网站可以使用 JavaScript 渲染,我必须自己渲染它,所以我使用了支持 JS 的 requests_html 库。不幸的是,它也没有奏效。我写的下面的示例代码应该打印该站点的第一个产品https://aptekazawiszy.pl/strony/wyszukiwanie.html?name=myd%C5%82o%20z%20olejem%20konopnym&pf-size=32&pf-page=1,但打印无。

from requests_html import HTMLSession

session = HTMLSession()

def get_prod_link(prod):
    p = prod[0].replace(' ','%20')
    url = f'https://aptekazawiszy.pl/strony/wyszukiwanie.html?name={p}&pf-size=32&pf-page=1'
    r = session.get(url)
    r.html.render(sleep=1, keep_page=True, scrolldown=1)
    xpath = '//*[@id="content"]/aside/div[10]/div[2]/div[1]/article/div/figure/a/img'
    elem = r.html.xpath(xpath) 
    return elem


if __name__ == '__main__':
    product = "mydlo z olejem konopnym"
    print(get_prod_link(product))

标签: pythonweb-scrapingpython-requests-html

解决方案


推荐阅读