Я пытаюсь собрать информацию с помощью Scrapy с сайта кроссовок для университетского проекта. Идея состоит в том, чтобы сказать Scrapy, чтобы он следовал по каждой ссылке на туфельку и очищал четыре информационных пункта (name, release_date, retail_price, resell_price). Затем go вернитесь на предыдущий сайт и нажмите на следующую ссылку и сделайте то же самое снова. В конце страницы перейдите на следующую страницу и повторяйте до тех пор, пока не останется больше ссылок.
Однако я всегда сталкиваюсь с сообщениями DEBUG и ERROR , когда Scrapy пытается достичь заданной start_url.
2020-04-06 11:59:56 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://stockx.com/sneakers/release-date?page=1> (referer: None)
2020-04-06 11:59:56 [scrapy.core.scraper] ERROR: Spider error processing <GET https://stockx.com/sneakers/release-date?page=1> (referer: None)
Вот код:
import scrapy
class Spider200406Item(scrapy.Item):
link = scrapy.Field()
name = scrapy.Field()
release_date = scrapy.Field()
retail_price = scrapy.Field()
resell_price = scrapy.Field()
class Spider200406Spider(scrapy.Spider):
name = 'spider_200406'
allowed_domains = ['www.stockx.com']
start_urls = ['https://stockx.com/sneakers/release-date?page=1']
BASE_URL = 'https://stockx.com/sneakers/release-date'
def parse(self, response):
links = response.xpath('//a[@class="TileBody-sc-1d2ws1l-0 bKAXcS"/@href').extract()
for link in links:
absolute_url = self.BASE_URL + link
yield scrapy.Request(absolute_url, callback=self.parse_info)
def parse_info(self, response):
item = Spider200406Item()
item["link"] = response.url
item["name"] = "".join(response.xpath("//h1[@class='name']//text()").extract())
item["release_date"] = "".join(response.xpath("//span[@data-testid='product-detail-release date']//text()").extract())
item["retail_price"] = "".join(response.xpath("//span[@data-testid='product-detail-retail price']//text()").extract())
item["resell_price"] = "".join(response.xpath("//div[@class='gauge-value']//text()").extract())
return item
- То, что я пробовал до сих пор:
- Измененный пользовательский агент не по умолчанию
- Изменил ROBOTSTXT_OBEY на False в settings.py
- Изменил DOWNLOAD_DELAY на 7 в settings.py
- Использовал оболочку Scrapy для получения дополнительной информации, получая пустую ссылку при сообщении для просмотра (ответ)
- Проверяется, зашифрована ли информация для очистки JavaScript (не следует)
- Изменены start_urls на start_url, Scrapy не принимает
- Went через VPN-соединение
Я также пробовал ту же структуру кода с гораздо более простым веб-сайтом. Тем не менее, я получаю то же сообщение об ошибке, которое приводит меня к выводу, что с кодом должно быть что-то не так.
Весь след:
2020-04-06 14:33:02 [scrapy.core.engine] INFO: Spider opened
2020-04-06 14:33:02 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-04-06 14:33:02 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-04-06 14:33:03 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://stockx.com/sneakers/release-date?page=1> (referer: None)
2020-04-06 14:33:03 [scrapy.core.scraper] ERROR: Spider error processing <GET https://stockx.com/sneakers/release-date?page=1> (referer: None)
Traceback (most recent call last):
File "/Applications/anaconda3/lib/python3.7/site-packages/parsel/selector.py", line 238, in xpath
**kwargs)
File "src/lxml/etree.pyx", line 1581, in lxml.etree._Element.xpath
File "src/lxml/xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
File "src/lxml/xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Invalid predicate
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/utils/defer.py", line 117, in iter_errback
yield next(it)
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/utils/python.py", line 345, in __next__
return next(self.data)
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/utils/python.py", line 345, in __next__
return next(self.data)
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/core/spidermw.py", line 64, in _evaluate_iterable
for r in iterable:
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/spidermiddlewares/offsite.py", line 29, in process_spider_output
for x in result:
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/core/spidermw.py", line 64, in _evaluate_iterable
for r in iterable:
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/spidermiddlewares/referer.py", line 338, in <genexpr>
return (_set_referer(r) for r in result or ())
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/core/spidermw.py", line 64, in _evaluate_iterable
for r in iterable:
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr>
return (r for r in result or () if _filter(r))
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/core/spidermw.py", line 64, in _evaluate_iterable
for r in iterable:
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr>
return (r for r in result or () if _filter(r))
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/core/spidermw.py", line 64, in _evaluate_iterable
for r in iterable:
File "/Users/ritterm/Desktop/Data2Dollar_Coding/Group_project/stockx_200406/stockx_200406/spiders/spider_200406.py", line 20, in parse
links = response.xpath('//a[@class="TileBody-sc-1d2ws1l-0 bKAXcS"/@href').extract()
File "/Applications/anaconda3/lib/python3.7/site-packages/scrapy/http/response/text.py", line 117, in xpath
return self.selector.xpath(query, **kwargs)
File "/Applications/anaconda3/lib/python3.7/site-packages/parsel/selector.py", line 242, in xpath
six.reraise(ValueError, ValueError(msg), sys.exc_info()[2])
File "/Applications/anaconda3/lib/python3.7/site-packages/six.py", line 692, in reraise
raise value.with_traceback(tb)
File "/Applications/anaconda3/lib/python3.7/site-packages/parsel/selector.py", line 238, in xpath
**kwargs)
File "src/lxml/etree.pyx", line 1581, in lxml.etree._Element.xpath
File "src/lxml/xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
File "src/lxml/xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
ValueError: XPath error: Invalid predicate in //a[@class="TileBody-sc-1d2ws1l-0 bKAXcS"/@href
2020-04-06 14:33:03 [scrapy.core.engine] INFO: Closing spider (finished)
Любые предложения и идеи высоко ценятся , MR