разбивка на страницы scrapy-splash
Может кто-нибудь помочь мне отладить этот код?
Идея состоит в том, чтобы получить доступ ко всем страницам и собрать информацию обо всех продуктах, но я застрял на нумерации страниц. Мне нужно получить доступ к страницам с помощью http с помощью или s, поэтому у моего xpath есть замена.
Я также пытался иметь дело с xpath, который иногда бывает @href, а иногда @url. Я обычно тестирую одно за другим, прежде чем группировать их все вместе.
Таким образом, parse_link, который я тестировал перед тем, как попробовать условие if, работает, и parse_product также работает.
У меня вопрос, где я ошибаюсь?
(Для записи, мне пришлось использовать заставку, чтобы избежать 403)
import scrapy
from scrapy_splash import SplashRequest
import scrapy_splash
class ProdutosSpider(scrapy.Spider):
name = 'teste27'
script = '''
function main(splash, args)
splash:on_request(function(request)
request:set_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36')
end)
splash.images_enabled = false
splash.js_enabled = false
splash.private_mode_enabled = false
assert(splash:go(args.url))
assert(splash:wait(7))
return splash:html()
end
'''
def start_requests(self):
yield SplashRequest(
url="http://www.auto-doc.pt/search?brandNo%5B0%5D=10668&page=1&sort_categories%5B0%5D=13",
callback=self.parse_link,
endpoint="execute",
args={'lua_source': self.script}
)
def parse_link(self, response):
for autodoc in response.xpath("//ul[@class='list_products']/li"):
url = str(autodoc.xpath(
".//div[@class='name']/span/@url").get()).replace("https", "http"),
link = str(autodoc.xpath(
".//div[@class='name']/a/@href").get()).replace("https", "http"),
if url:
yield SplashRequest(
url=url,
callback=self.parse_produto,
endpoint="execute",
args={'lua_source': self.script}
)
else:
yield SplashRequest(
url=link,
callback=self.parse_produto,
endpoint="execute",
args={'lua_source': self.script}
)
next_page = str(response.xpath("//span[@class='next']/a/@href").get()).replace("https", "http")
if next_page:
yield SplashRequest(
url=next_page,
callback=self.parse_link,
endpoint="execute",
args={'lua_source': self.script}
)
def parse_produto(self, response):
for produtos in response.xpath("//main[@id='content']/div"):
yield{
'produto': produtos.xpath(".//span[@class='title']/text()").get(),
'cod_fab': produtos.xpath(".//span[@class='subtitle-art-nummer']/span/text()").get(),
'oem_url': produtos.xpath(".//div[@class='oem-list']/div/ul/li/span/text()").getall(),
'oem': produtos.xpath(".//div[@class='oem-list']/div/ul/li/a/text()").getall(),
'ean': produtos.xpath(".//ul[@class='product-block__description__eom']/li/span/text()").getall()
}