所以我的问题是我有一个爬行蜘蛛
name = 'recursiveSpider'
allowed_domains = ['industrialnetworking.com']
custom_settings = {
'DUPEFILTER_CLASS' : 'scrapy.dupefilters.BaseDupeFilter',
}
start_urls = [
'https://www.industrialnetworking.com/Manufacturers/Hirschmann'
]
rules = (
Rule(LinkExtractor(restrict_css='div.catCell a::attr(href)'), follow=True),
Rule(LinkExtractor(allow=r"/Manufacturers/Hirschmann*"), callback='parse_new_item')
)
我正在尝试访问所有“Hirshmann”产品的产品页面。我知道我的错误是在“规则”的第二行,在那里我允许任何带有Hirschmann* 的内容。尽管我不确定如何添加response.css/response.xpath作为允许的参数。
理想情况下,我希望它,如果爬虫所有“div.catCell a:attr(href)”,并递归通过他们,直到它检测到“response.css('td.cellDesc h2 a::attr(href;')",然后它会发送该链接到我的“parse_new_item”。如果该项目没有找到,然后继续以下所有链接,有“div.catCell a:attr(href)"。
Example URL travel path ->
StartURL: https://www.industrialnetworking.com/Manufacturers/Hirschmann
Category: https://www.industrialnetworking.com/Manufacturers/Hirschmann-Rail-Switches
SubCategory: https://www.industrialnetworking.com/Manufacturers/Hirschmann-Switches-Unmanaged
Series: https://www.industrialnetworking.com/Manufacturers/Hirschmann-SPIDER-Family-Rail-Switches
END GOAL ->
Product: https://www.industrialnetworking.com/Manufacturers/Hirschmann-SPIDER-III-Rail-Switches/Hirschmann-SSL20-5TX-Rail-Switch-942-132-001
编辑-我的目标xpath/css路径的原因是因为链接没有任何明显的模式,我可以用来目标的url。
谢谢大家!
2条答案
按热度按时间1zmg4dgp1#
我个人并不是
crawlspider
的忠实粉丝。在一些情况下,它很方便,但我认为在你的情况下,坚持手动爬取链接可能是一种更容易的方法。由于您有多个具有相同格式的页面,因此您可以将每个链接反馈回主
parse
方法,直到它找到与td/h2/a
链接相匹配的链接,此时它可以使用parse_new_item
方法分配一个不同的回调来解析最终的产品页面。例如:
输出很长,所以我把最后的计数放在下面。
输出
at0kjp5o2#
你上面提到的网页包含14个列表urls.So你可以使用xpath或css选择器只和你必须使用
follow = False
摆脱不必要的url