Здравствуйте, я новичок в Scrapy.И я пытаюсь использовать scrapy с scrapyd
Я пытаюсь удалить эти два веб-сайта, и почему-то один из веб-сайтов не работает, хотя я использую в основном тот же файл spider.py.Только меняя некоторые
Пожалуйста, помогите
Скрап
#the Working spider
scrapyd.schedule("default", "detik", settings=settings, url=url, domain=domain)
#the NOT Working spider
scrapyd.schedule("default", "kompas", settings=settings, url=url, domain=domain)
Рабочий код паука
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class NewsSpider(CrawlSpider):
name = 'detik'
def __init__(self, *args, **kwargs):
self.url = kwargs.get('url')
self.domain = kwargs.get('domain')
self.start_urls = [self.url]
self.allowed_domains = [self.domain]
print("READY TO GO TO DETIK")
def parse(self, response):
print("INSIDE OF DETIK")
urls = response.xpath("//article/div/a/@href").extract()
for url in urls:
print(url)
yield scrapy.Request(url=url, callback=self.parse_detail)
def parse_detail(self, response):
print("SCRAP EVERY LINK DETIK")
i = {}
i['breadcrumbs'] = response.xpath("//div[@class='breadcrumb']/a/text()").extract()
i['tanggal'] = response.xpath("//div[@class='date']/text()").extract_first()
i['penulis'] = response.xpath("//div[@class='author']/text()").extract_first()
i['judul'] = response.xpath("//h1/text()").extract_first()
i['berita'] = response.xpath("normalize-space(//div[@class='detail_text'])").extract_first()
i['tag'] = response.xpath("//div[@class='detail_tag']/a/text()").extract()
i['url'] = response.request.url
i['website'] = 'detik'
return i #Goes to Pipeline
НЕ РАБОТАЮЩИЙ код паука
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class KompasSpider(CrawlSpider):
name = 'kompas'
def __init__(self, *args, **kwargs):
self.url = kwargs.get('url')
self.domain = kwargs.get('domain')
self.start_urls = [self.url]
self.allowed_domains = [self.domain]
print("READY TO GO TO KOMPAS")
def parse(self, response):
print("INSIDE OF KOMPAS")
urls = response.xpath("//h3/a/@href").extract()
for url in urls:
print(url)
yield scrapy.Request(url=url, callback=self.parse_detail)
def parse_detail(self, response):
print("SCRAP EVERY LINK KOMPAS")
i = {}
i['breadcrumbs'] = response.xpath("//a/span/text()").extract()
i['tanggal'] = response.xpath("substring(//div[@class='read__time']/text(),14,21)").extract_first()
i['penulis'] = response.xpath("//div[@class='read__author']/a/text()").extract_first()
i['judul'] = response.xpath("//h1[@class='read__title']/text()").extract_first()
i['berita'] = response.xpath("normalize-space(//div[@class='read__content'])").extract_first()
i['tag'] = response.xpath("//li/a[@class='tag__article__link']/text()").extract()
i['url'] = response.request.url
i['website'] = 'kompas'
return i
Я думаю, проблема в том, что они не проходят через parse_detail.причина, по которой печать («УСТРАЖДАТЬ КАЖДУЮ ССЫЛКУ КОМПАС») не печатается в журнале.