python - 为什么我无法通过 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))
解决方案
推荐阅读
- ios - 尝试将多个 Uitextfields 添加为整数
- javascript - 预注册 Cognito 触发器
- bitbucket - BitBucket API 获取存储库管理员
- android - 如何从两种不同的方式(房间、用户)处理 Android LiveData 更改?
- java - 如何检查给定的firebase引用是否不包含任何值或为空
- tomcat - 如何在 tomcat 7 中获取映射到 index.html 的基本 url?
- sql-server - 如何抑制sql查询输出并仅显示行数
- vim - 如何格式化某些行中包含空列的表格?
- excel - 查找与值关联的最后一个非空白单元格
- powerbi - Power Bi,按月比较文本列