【问题标题】:Python Web Scraping Dynamic ContentPython 网页抓取动态内容
【发布时间】:2020-05-24 02:55:49
【问题描述】:

我一直在尝试抓取 kith.com 的搜索结果,但我得到了骨架示例代码。尝试使用 scrapy、requests-html 和 selenium,但我没有设法让它们工作。

现在我的代码是:

from requests_html import HTMLSession

session = HTMLSession()
r = session.get("https://kith.com/pages/search-results-page?q=nike&tab=products&sort_by=created")

r.html.render()
print(r)

据我所见,render() 应该得到在浏览器中看到的 html 代码,但我仍然得到相同的“原始”代码。

PD:kith.com 是 shopify 商店

【问题讨论】:

标签: python web-scraping python-requests python-requests-html


【解决方案1】:

Selenium适合这样的工作

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get('https://kith.com/pages/search-results-page?q=nike&tab=products&sort_by=created')


item_titles = driver.find_elements_by_class_name("snize-title")

print item_titles[0].text
#NIKE WMNS SHOX TL - NOVA WHITE / TEAM ORANGE / SPRUCE AURA

编辑:

如果您想捕获所有项目信息,则具有 snize-overhidden 类的 div 元素将是您想要捕获的内容。然后你可以遍历它们和它们的子元素

【讨论】:

  • 在计算机不必打开任何浏览器的情况下我将如何做到这一点?我的目的是在项目完成后将其上传到 AWS,以便它可以每隔几个小时运行一次
  • 浏览器可以在无头模式下运行(它在后台运行)。检查更新的答案@NyTrOuS
  • 复制了您的代码,我收到一条错误消息,指出未定义“选项”
猜你喜欢
  • 1970-01-01
  • 2013-07-10
  • 2023-01-29
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 2019-07-19
相关资源
最近更新 更多