crawl spider

crawlspider

使用
scrapy genspider -t crawl 文件名字 网址


crawlspider是什么?
也是一个spider,是Spider的一个子类,所以其功能要比Spider要强大
多的一个功能是:提取链接的功能,根据一定的规则,提取指定的链接


链接提取器
LinkExtractor(
allow=xxx, # 正则表达式,要(*)
deny=xxx, # 正则表达式,不要这个
restrict_xpaths=xxx, # xpath路径(*)
restrict_css=xxx, # 选择器(*)
deny_domains=xxx, # 不允许的域名
)

通过正则提取链接
links = LinkExtractor(allow=r'/movie/\?page=\d')
将所有包含这个正则表达式的href全部获取到返回
links.extract_links(response)进行查看提取到的链接
【注】将重复的url去除掉
通过xpath提取
links = LinkExtractor(restrict_xpaths='//ul[@class="pagination pagination-sm"]/li/a')
通过css提取
links = LinkExtractor(restrict_css='.pagination > li > a')

猜你喜欢

转载自www.cnblogs.com/airapple/p/9195467.html