class scrapy.spiders.Spider
字段和说明 |
name
这是你的爬虫的名字。
|
allowed_domains
这是爬虫爬行的域列表。
|
start_urls
它是一个 URL 列表,它将是以后爬行的根,爬虫将从那里开始爬行。
|
custom_settings
这些设置,当运行爬虫时,将被项目范围的配置覆盖。
|
crawler
它是链接到爬虫实例绑定的Crawler对象的属性。
|
settings
这些是运行爬虫的设置。
|
logger
它是一个用于发送日志消息的Python logger。
|
from_crawler(crawler,*args,**kwargs)
它是一个类方法,它创建你的爬虫。参数是-
crawler-爬虫实例将绑定到的爬虫。 args(list)-这些参数被传递给方法 _init_()。 kwargs(dict)-这些关键字参数被传递给方法 _init_()。 |
start_requests()
当没有指定特定的 URL 并且打开爬虫进行抓取时,Scrapy 调用
start_requests() 方法.
|
make_requests_from_url(url)
是一种用于将url转换为请求的方法。
|
parse(response)
此方法处理响应并在更多 URL 之后返回废弃的数据。
|
log(message[,level,component])
是一种通过spider logger发送日志消息的方法。
|
closed(reason)
爬虫关闭时调用此方法。
|
scrapy crawl first_scrapy-a group = accessories
import scrapy class FirstSpider(scrapy.Spider): name = "first" def __init__(self, group = None, *args, **kwargs): super(FirstSpider, self).__init__(*args, **kwargs) self.start_urls = ["http://www.example.com/group/%s" % group]
import scrapy from scrapy.item import Item, Field class First_scrapyItem(scrapy.Item): product_title = Field() product_link = Field() product_description = Field()
class scrapy.spiders.CrawlSpider
规则和说明 |
LinkExtractor
它指定爬虫如何跟随链接并提取数据。
|
callback
每次抓取页面后都会调用。
|
follow
指定是否继续关注链接。
|
import scrapy from scrapy.spiders import CrawlSpider, Rule from scrapy.linkextractors import LinkExtractor class DemoSpider(CrawlSpider): name = "demo" allowed_domains = ["www.demoexample.com"] start_urls = ["http://www.demoexample.com"] rules = ( Rule(LinkExtractor(allow =(), restrict_xpaths = ("//div[@class = 'next']",)), callback = "parse_item", follow = true), ) def parse_item(self, response): item = DemoItem() item["product_title"] = response.xpath("a/text()").extract() item["product_link"] = response.xpath("a/@href").extract() item["product_description"] = response.xpath("div[@class = 'desc']/text()").extract() return items
class scrapy.spiders.XMLFeedSpider
属性和描述 |
iterator
它定义了要使用的迭代器。它可以是
iternodes、html 或
xml。默认为
iternodes。
|
itertag
要迭代的节点名字符串。
|
namespaces
它由 (prefix, uri) 元组列表定义,使用
register_namespace() 方法自动注册命名空间。
|
adapt_response(response)
它接收响应并在它从爬虫中间件到达时立即修改响应正文,然后爬虫开始解析它。
p> |
parse_node(response,selector)
当为匹配提供的标签名称的每个节点调用时,它接收响应和选择器。
注意-如果您不覆盖此方法,您的爬虫将无法工作。
|
process_results(response,results)
它返回爬虫返回的结果和响应的列表。
|
class scrapy.spiders.CSVFeedSpider
选项和说明 |
delimiter
它是一个字符串,每个字段包含一个逗号(',')分隔符。
|
quotechar
它是一个包含每个字段的引号('"')的字符串。
|
headers
这是一个可以从中提取字段的语句列表。
|
parse_row(response,row)
它接收一个响应和每一行以及标题的键。
|
from scrapy.spiders import CSVFeedSpider from demoproject.items import DemoItem class DemoSpider(CSVFeedSpider): name = "demo" allowed_domains = ["www.demoexample.com"] start_urls = ["http://www.demoexample.com/feed.csv"] delimiter = ";" quotechar = "'" headers = ["product_title", "product_link", "product_description"] def parse_row(self, response, row): self.logger.info("this is row: %r", row) item = DemoItem() item["product_title"] = row["product_title"] item["product_link"] = row["product_link"] item["product_description"] = row["product_description"] return item
class scrapy.spiders.SitemapSpider
字段和说明 |
sitemap_urls
要抓取的指向站点地图的 URL 列表。
|
sitemap_rules
是元组列表(regex,callback),其中regex是正则表达式,callback用于处理匹配正则的url表达。
|
sitemap_follow
这是要遵循的站点地图正则表达式列表。
|
sitemap_alternate_links
指定单个网址要遵循的备用链接。
|
from scrapy.spiders import SitemapSpider class DemoSpider(SitemapSpider): urls = ["http://www.demoexample.com/sitemap.xml"] def parse(self, response): # You can scrap items here
from scrapy.spiders import SitemapSpider class DemoSpider(SitemapSpider): urls = ["http://www.demoexample.com/sitemap.xml"] rules = [ ("/item/", "parse_item"), ("/group/", "parse_group"), ] def parse_item(self, response): # you can scrap item here def parse_group(self, response): # you can scrap group here
from scrapy.spiders import SitemapSpider class DemoSpider(SitemapSpider): urls = ["http://www.demoexample.com/robots.txt"] rules = [ ("/company/", "parse_company"), ] sitemap_follow = ["/sitemap_company"] def parse_company(self, response): # you can scrap company here
from scrapy.spiders import SitemapSpider class DemoSpider(SitemapSpider): urls = ["http://www.demoexample.com/robots.txt"] rules = [ ("/company/", "parse_company"), ] other_urls = ["http://www.demoexample.com/contact-us"] def start_requests(self): requests = list(super(DemoSpider, self).start_requests()) requests += [scrapy.Request(x, self.parse_other) for x in self.other_urls] return requests def parse_company(self, response): # you can scrap company here... def parse_other(self, response): # you can scrap other here...