【发布时间】:2018-04-03 17:37:49
【问题描述】:
我一直在尝试从 Steam 商店中抓取生化奇兵游戏并将其名称、价格和链接保存在 CSV 文件中。我知道如何通过使用 Scrapy 来做到这一点,但我真的很想知道是否有一种方法可以结合 Scrapy 和 Selenium 来做到这一点。我想使用 Selenium 只是为了摆脱某些游戏商店网站上弹出的年龄检查门。
所以我已经设法通过使用 Scrapy 来抓取没有年龄限制的游戏,并且我已经成功地使用 Selenium 绕过了年龄限制。
我遇到的问题是将 Selenium 通过绕过年龄门打开的游戏商店网站传递给 Scrapy,以便它可以抓取它。由于一切正常,我得出的结论是问题在于我不知道如何连接它们。
def parse_product(self, response):
product = ScrapesteamItem()
sel = self.driver
#Passing first age gate
if '/agecheck/app/' in response.url:
sel.get(response.url)
select = Select(sel.find_element_by_xpath("""//*[@id="ageYear"]"""))
select.select_by_visible_text("1900")
sel.find_element_by_xpath("""//*[@id="agecheck_form"]/a""").click()
#Pass Selenium newly opened site to Scrapy
#Passing second age gate
elif '/agecheck' in response.url:
sel.get(response.url)
sel.find_element_by_xpath("""//*[@id="app_agegate"]/div[3]/a[1]""").click()
#Pass Selenium newly opened site to Scrapy
#Scraping the data with scrapy
else:
name = response.css('.apphub_AppName ::text').extract()
price = response.css('div.game_purchase_price ::text, div.discount_final_price ::text').extract()
link = response.css('head > link:nth-child(40) ::attr(href)').extract()
for product in zip(name, price, link):
scrapedInfo = {
'NAME' : product[0],
'PRICE' : product[1].rstrip().lstrip(),
'LINK' : product[2]
}
yield scrapedInfo
我希望有人知道怎么做(如果可能的话)。
附:我知道有更好的方法来抓取 Steam 商店,我知道可能有一个 API,但在我去了解之前,我想知道是否有办法做到这一点,即使它不是最理想的。
【问题讨论】:
-
我强烈建议使用
BeautifulSoup和selenium的组合。使用selenium通过自动浏览器与网页进行交互,并将页面源HTML传递给BeautifulSoup以解析和提取您想要的内容。 -
当然,我会试试的。你能告诉我如何在 Selenium 中传递页面源 HTML,为什么你更喜欢 BeautifulSoup 而不是 Scrapy?
标签: python selenium web-scraping scrapy