Python 为什么我的Scrapy规则(LinkExtractor)不起作用?
这是我在堆栈溢出中遇到的第一个问题。我开始在工作中使用Python来刮取数据,并一直使用Scrapy来完成这些任务。我试图为一个政府网站设置一个刮板,但我没有输出。最初,我在rules变量中设置了三条规则,但json文件将显示为空。代码很好,但我不知道出了什么问题。感谢您能分享的任何见解。祝你今天愉快Python 为什么我的Scrapy规则(LinkExtractor)不起作用?,python,web-scraping,scrapy,Python,Web Scraping,Scrapy,这是我在堆栈溢出中遇到的第一个问题。我开始在工作中使用Python来刮取数据,并一直使用Scrapy来完成这些任务。我试图为一个政府网站设置一个刮板,但我没有输出。最初,我在rules变量中设置了三条规则,但json文件将显示为空。代码很好,但我不知道出了什么问题。感谢您能分享的任何见解。祝你今天愉快 import scrapy from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpid
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
class DirSpider(CrawlSpider):
name = 'di7'
allowed_domains = ['transparencia.gob.sv']
start_urls = ['https://www.transparencia.gob.sv/categories/2']
rules = (
Rule(LinkExtractor(restrict_css=".filtrable a"), callback='parse_item', follow=True),
Rule(LinkExtractor(restrict_css="a:nth-of-type(19)"), callback='parse_item', follow=True),
)
def parse(self, response):
items = {}
css_selector = response.css(".spaced .align-justify")
for bureaucrat in css_selector:
name = bureaucrat.css(".medium-11 a::text").extract_first()
charge = bureaucrat.css(".medium-12::text").extract_first()
status = bureaucrat.css(".medium-11 .text-mutted::text").extract_first()
institution = response.css("small::text").extract()
items['name'] = name
items['charge'] = charge
items['status'] = status
items['institution'] = institution
yield(items)```
尝试将解析函数重命名为parse_项:
你们有parse_项回调吗?是的,我把它放在我的规则代码中。我的意思是你们定义parse_项时是否像定义parse一样?不,我没有。我应该在哪里定义它?在items.py文件中,还是应该将其作为函数写入此文件中?我基本上是在我的课程中应用了这个蜘蛛的相同模板你在钱上是对的!!谢谢你,伙计!!
def parse_item(self, response):
items = {}
css_selector = response.css(".spaced .align-justify")
for bureaucrat in css_selector:
name = bureaucrat.css(".medium-11 a::text").extract_first()
charge = bureaucrat.css(".medium-12::text").extract_first()
status = bureaucrat.css(".medium-11 .text-mutted::text").extract_first()
institution = response.css("small::text").extract()
items['name'] = name
items['charge'] = charge
items['status'] = status
items['institution'] = institution
yield(items)