문제 설명
Scrapy Spider를 사용하는 Craigslist Scraper가 기능을 수행하지 않음 (Craigslist Scraper using Scrapy Spider not performing functions)
2021‑05‑07 10:07:14 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://tampa.craigslist.org/robots.txt> (referer: None)
2021‑05‑07 10:07:14 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://tampa.craigslist.org/d/cell‑phones/search/moa/> (referer: None)
2021‑05‑07 10:07:19 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://tampa.craigslist.org/d/cell‑phones/search/moa?s=120> (referer: https://tampa.craigslist.org/d/cell‑phones/search/moa/)
2021‑05‑07 10:07:21 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://tampa.craigslist.org/d/cell‑phones/search/moa?s=240> (referer: https://tampa.craigslist.org/d/cell‑phones/search/moa?s=120)
이것은 내가 얻은 출력입니다. 다음 버튼을 선택하고 27행에서 요청을 수행하여 수행되는 결과 페이지로 이동하는 것처럼 보입니다.
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import Rule, Request
from craig.items import CraigItem
from scrapy.selector import Selector
class PhonesSpider(scrapy.Spider):
name = 'phones'
allowed_domains = ['tampa.craigslist.org']
start_urls = ['https://tampa.craigslist.org/d/cell‑phones/search/moa/']
def parse(self, response):
phones = response.xpath('//p[@class="result‑info"]')
for phone in phones:
relative_url = phone.xpath('a/@href').extract_first()
absolute_url = response.urljoin(relative_url)
title = phone.xpath('a/text()').extract_first()
price = phone.xpath('//*[@id="sortable‑results"]/ul/li[3]/a/span').extract_first()
yield Request(absolute_url, callback=self.parse_item, meta={'URL': absolute_url, 'Title': title, 'price': price})
relative_next_url = response.xpath('//a[@class="button next"]/@href').extract_first()
absolute_next_url = "https://tampa.craigslist.org" + relative_next_url
yield Request(absolute_next_url, callback=self.parse)
def parse_item(self, response):
item = CraigItem()
item["cl_id"] = response.meta.get('Title')
item["price"] = response.meta.get
absolute_url = response.meta.get('URL')
yield{'URL': absolute_url, 'Title': title, 'price': price}
내 코드에서와 같이 전화 루프의 전화가 실행되지 않아 parse_item을 실행하지 않고 다음 URL을 계속 요청하고 있습니다. 일부 자습서를 따르고 문서를 읽고 있지만 여전히 내가 뭘 잘못하고 있는지 파악하는 데 문제가 있습니다. 어렸을 때 취미로 arduinos를 코딩한 경험이 있지만 전문적인 코딩 경험이 없습니다. 이것이 이와 같은 프로젝트에 대한 저의 첫 번째 장점입니다. 루프, 함수, 콜백 등의 기본 사항은 충분히 이해하고 있습니다.
도움을 주시면 감사하겠습니다.
업데이트
현재 출력
참조 솔루션
방법 1:
Class result‑info
is used within the div
block, so you should write:
phones = response.xpath('//div[@class="result‑info"]')
That being said, I didn't check/fix your spider further (it seems there are only parsing errors, not functional ones). As a suggestion for the future, you can use Scrapy shell for quickly debugging the issues:
scrapy shell "your‑url‑here"
(by fnctrloptcmd、Serhii Shynkarenko)