Learning Scrapy 笔记

1. 命令行在项目根目录,根据crawl模版创建名为test的spider,web指spider可爬取的域名url,
   scrapy提供了诸多模版
    scrapy genspider –t crawl test web

2. scrapy抓取中文结果默认是unicode,无法显示中文
    在setting设置 FEED_EXPORT_ENCODING = 'utf-8'

3. 并发下载数量限制,默认为8个
    CONCURRENT_REQUESTS_PER_DOMAIN = 1

4. 请求间的延迟
    DOWNLOAD_DELAY = 5

5. 请求不去重
    scrapy.Request()增加参数 dont_filter = True

6. ValueError: Missing scheme in request url
    scrapy.Request(response.urljoin(link))

7. scrapy shell 报错403 shell里加个UA头
    scrapy shell -s USER_AGENT='Mozilla/5.0' url_link

8. 代码没问题,取不到数据,无报错,一般是xpath有问题,可使用scrapy shell调试xpath
    xpath在chrome能取到数据,在scrapy不一定能取到

9. 爬取到指定数量的item后关闭爬虫,scrapy也提供了定时关闭方法
    CLOSESPIDER_ITEMCOUNT = 10

10. 数据消除空格和'\n'
    extract()获取到列表后使用[0]切片,使用strip()方法 .extract()[0].strip()
    .replace(old_str, new_str)    替换字符串
    .split(' / ')    删除指定参数,返回切割后的字符串列表

猜你喜欢

转载自blog.csdn.net/qq_18525247/article/details/81987730