这个问题有点像 this 的后续问题我之前问过的问题。
我正在尝试抓取一个首页包含一些链接的网站。类似于 this 的东西.
现在,因为我想抓取页面上显示的项目的详细信息,所以我提取了它们各自的 URL。
我已将这些 URL 保存在列表中。
如何启动蜘蛛程序来单独抓取页面?
为了更好的理解:
[urlA, urlB, urlC, urlD...]
这是我抓取的 URL 列表。现在我想启动一个蜘蛛来单独抓取链接。
我该怎么做?
请您参考如下方法:
我假设您要访问的网址指向具有相同或相似结构的页面。如果是这种情况,您应该这样做:
from scrapy.contrib.spiders import CrawlSpider
from scrapy.selector import Selector
from scrapy.http import Request
class YourCrawler(CrawlSpider):
name = 'yourCrawler'
allowed_domains = 'domain.com'
start_urls = ["htttp://www.domain.com/example/url"]
def parse(self, response):
#parse any elements you need from the start_urls and, optionally, store them as Items.
# See http://doc.scrapy.org/en/latest/topics/items.html
s = Selector(response)
urls = s.xpath('//div[@id="example"]//a/@href').extract()
for url in urls:
yield Request(url, callback=self.parse_following_urls, dont_filter=True)
def parse_following_urls(self, response):
#Parsing rules go here
否则,如果您要遵循的 url 指向具有不同结构的页面,那么您可以为它们定义特定的方法(例如 parse1、parse2、parse3...)。
