这对有经验的用户来说可能很容易,但我对scrapy是新手,我想要的是一个爬行器,它可以爬到用户定义的页面上。现在我正试图修改allow pattern
中的__init__
,但似乎不起作用。目前我的代码摘要是:
class MySpider(CrawlSpider):
name = "example"
allowed_domains = ["example.com"]
start_urls = ["http://www.example.com/alpha"]
pattern = "/[\d]+$"
rules = [
Rule(LinkExtractor(allow=[pattern] , restrict_xpaths=('//*[@id = "imgholder"]/a', )), callback='parse_items', follow=True),
]
def __init__(self, argument='' ,*a, **kw):
super(MySpider, self).__init__(*a, **kw)
#some inputs and operations based on those inputs
i = str(raw_input()) #another input
#need to change the pattern here
self.pattern = '/' + i + self.pattern
#some other operations
pass
def parse_items(self, response):
hxs = HtmlXPathSelector(response)
img = hxs.select('//*[@id="imgholder"]/a')
item = MyItem()
item["field1"] = "something"
item["field2"] = "something else"
yield item
pass
现在假设用户输入i=2
,所以我想转到以/2/*some number*
结尾的URL,但是现在发生的是爬行器正在爬行任何模式/*some number
。更新似乎没有传播。我正在使用scrapy version 1.0.1
。你知道吗
有办法吗?提前谢谢。你知道吗
当您拥有名为
__init__
的Rule
方法时,已经使用开头定义的模式进行了设置。你知道吗但是,您可以在
__init__
方法中动态更改它。为此,在方法体中再次设置Rule
并编译它(类似于这样的内容):相关问题 更多 >
编程相关推荐