Web crawler scrapy中的动态启动URL
我正在使用scrapy在一个站点上抓取多个页面。 变量Web crawler scrapy中的动态启动URL,web-crawler,scrapy,Web Crawler,Scrapy,我正在使用scrapy在一个站点上抓取多个页面。 变量start\u url用于定义要爬网的页面。 我首先从第一页开始,因此在文件example\u spider.py 在从第一个页面获得更多信息后,我将确定下一个要爬网的页面,然后相应地分配start\u URL。因此,我必须用对start\u url=[1st page,2nd page,…,Kth page]的更改覆盖上面的示例_spider.py,然后再次运行scrapy crawl 这是最好的方法还是有更好的方法使用scrapy API
start\u url
用于定义要爬网的页面。
我首先从第一页开始,因此在文件example\u spider.py
在从第一个页面获得更多信息后,我将确定下一个要爬网的页面,然后相应地分配start\u URL
。因此,我必须用对start\u url=[1st page,2nd page,…,Kth page]
的更改覆盖上面的示例_spider.py,然后再次运行scrapy crawl
这是最好的方法还是有更好的方法使用scrapy API动态分配start\u URL
,而不必覆盖example\u splider.py
?
谢谢。
开始\u URL
类属性包含开始URL-仅此而已。如果您提取了其他页面的URL,您希望从parse
callback中获取,则使用[other]callback返回相应的请求:
class Spider(BaseSpider):
name = 'my_spider'
start_urls = [
'http://www.domain.com/'
]
allowed_domains = ['domain.com']
def parse(self, response):
'''Parse main page and extract categories links.'''
hxs = HtmlXPathSelector(response)
urls = hxs.select("//*[@id='tSubmenuContent']/a[position()>1]/@href").extract()
for url in urls:
url = urlparse.urljoin(response.url, url)
self.log('Found category url: %s' % url)
yield Request(url, callback = self.parseCategory)
def parseCategory(self, response):
'''Parse category page and extract links of the items.'''
hxs = HtmlXPathSelector(response)
links = hxs.select("//*[@id='_list']//td[@class='tListDesc']/a/@href").extract()
for link in links:
itemLink = urlparse.urljoin(response.url, link)
self.log('Found item link: %s' % itemLink, log.DEBUG)
yield Request(itemLink, callback = self.parseItem)
def parseItem(self, response):
...
如果仍要自定义开始请求创建,override方法是否为parseItem(self,response)返回该项?
parseItem
,因为任何其他回调都可以返回一个项
或另一个请求
,而另一个回调未定义我得到的请求,请帮助please@WilliamKinaan@warwaruk非常感谢。