如何访问此响应中的 "price" 标记?
How can I access the "price" tag within this response?
情况:
我正在尝试使用 scrapy 抓取广告网站。
我可以从与我相关的标签中访问和提取信息,但是,“价格”标签不愿意提供文本。
我试过的:
对于价格标签,我尝试了不同的方法,即 css 选择器和 xpath,但没有任何效果。
response.css('span.ma-AdPrice-value.ma-AdPrice-value--default.ma-AdPrice-value--heading--l::text').get()
response.xpath("//span[@class='ma-AdPrice-value ma-AdPrice-value--default ma-AdPrice-value--heading--l']/text()").get()
我在终端中使用 scrapy shell
尝试了不同的 response
调用,包括在较高标签级别提取数据,但没有成功。
代码:
下面是代码摘录。标题标签(和其他标签)工作正常,价格标签是这里的问题。
class AdSpider(scrapy.Spider):
name = 'ads'
start_urls = [
'https://www.milanuncios.com/volkswagen-de-segunda-mano/volkswagen-golf-2-0-tdi-184cv-dsg-gtd-bmt-409202986.htm'
]
def parse(self, response):
title = response.css('h1.ma-AdDetail-title.ma-AdDetail-title-size-heading-m::text').get()
price = response.css('span.ma-AdPrice-value.ma-AdPrice-value--default.ma-AdPrice-value--heading--l::text').get()
items['title'] = title
items['price'] = price
yield items
问题:
我没有网络抓取经验,所以网站 html 代码中是否有我没有看到的隐藏功能?
我该如何解决这个问题?
这里的问题似乎是页面需要 JavaScript。
当 运行 测试请求和 BeautifulSoup:
>>> import requests
>>> from bs4 import BeautifulSoup as bs
>>> res = requests.get("https://www.milanuncios.com/volkswagen-de-segunda-mano/volkswagen-golf-2-0-tdi-184cv-dsg-gtd-bmt-409202986.htm")
>>> soup = bs(res.text, "lxml")
>>> soup.find("span", text="Precio financiado")
找不到该项目,因为它引发了要求 javascript 继续机器人保护的错误。
使用硒收集时:
>>> from selenium import webdriver
>>> from bs4 import BeautifulSoup as bs
>>> path ="path_to_executable"
>>> driver = webdriver.Firefox(executable_path=path)
>>> driver.get("https://www.milanuncios.com/volkswagen-de-segunda-mano/volkswagen-golf-2-0-tdi-184cv-dsg-gtd-bmt-409202986.htm")
>>> soup = bs(driver.page_source, "lxml")
>>> soup.find("span", text="Precio financiado").findNext("span").text
返回预期价格。
要解决这个问题,您要么需要考虑将 selenium 整合到 scrapy 中,要么只使用 selenium 来收集数据。
情况:
我正在尝试使用 scrapy 抓取广告网站。 我可以从与我相关的标签中访问和提取信息,但是,“价格”标签不愿意提供文本。
我试过的:
对于价格标签,我尝试了不同的方法,即 css 选择器和 xpath,但没有任何效果。
response.css('span.ma-AdPrice-value.ma-AdPrice-value--default.ma-AdPrice-value--heading--l::text').get()
response.xpath("//span[@class='ma-AdPrice-value ma-AdPrice-value--default ma-AdPrice-value--heading--l']/text()").get()
我在终端中使用 scrapy shell
尝试了不同的 response
调用,包括在较高标签级别提取数据,但没有成功。
代码:
下面是代码摘录。标题标签(和其他标签)工作正常,价格标签是这里的问题。
class AdSpider(scrapy.Spider):
name = 'ads'
start_urls = [
'https://www.milanuncios.com/volkswagen-de-segunda-mano/volkswagen-golf-2-0-tdi-184cv-dsg-gtd-bmt-409202986.htm'
]
def parse(self, response):
title = response.css('h1.ma-AdDetail-title.ma-AdDetail-title-size-heading-m::text').get()
price = response.css('span.ma-AdPrice-value.ma-AdPrice-value--default.ma-AdPrice-value--heading--l::text').get()
items['title'] = title
items['price'] = price
yield items
问题: 我没有网络抓取经验,所以网站 html 代码中是否有我没有看到的隐藏功能? 我该如何解决这个问题?
这里的问题似乎是页面需要 JavaScript。
当 运行 测试请求和 BeautifulSoup:
>>> import requests
>>> from bs4 import BeautifulSoup as bs
>>> res = requests.get("https://www.milanuncios.com/volkswagen-de-segunda-mano/volkswagen-golf-2-0-tdi-184cv-dsg-gtd-bmt-409202986.htm")
>>> soup = bs(res.text, "lxml")
>>> soup.find("span", text="Precio financiado")
找不到该项目,因为它引发了要求 javascript 继续机器人保护的错误。
使用硒收集时:
>>> from selenium import webdriver
>>> from bs4 import BeautifulSoup as bs
>>> path ="path_to_executable"
>>> driver = webdriver.Firefox(executable_path=path)
>>> driver.get("https://www.milanuncios.com/volkswagen-de-segunda-mano/volkswagen-golf-2-0-tdi-184cv-dsg-gtd-bmt-409202986.htm")
>>> soup = bs(driver.page_source, "lxml")
>>> soup.find("span", text="Precio financiado").findNext("span").text
返回预期价格。
要解决这个问题,您要么需要考虑将 selenium 整合到 scrapy 中,要么只使用 selenium 来收集数据。