版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/j_bleach/article/details/78894148
前言
本文记录自己在学习scrapy当中遇到的各种大小问题,持续更新。
环境简介:
语言版本 | 爬虫框架 | IDE | 系统 |
---|---|---|---|
python3.5 | scrapy1.4.0 | pycharm | win10 x64 |
scrapy安装失败
报错显示缺少Twisted。 错误分析:Twisted本身是一个网络引擎框架,scrapy的运行依赖于Twisted。 解决办法:通过 http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted下载相关版本的Twisted的whl文件,在通过pip install xxxpath(xxxpath 指代whl下载到本机的路径)。
No module named scrapy
在pycharm打开scrapy新建的项目后,import scrapy
报错显示No module named scrapy。
错误分析:pycharm的默认依赖项当中没有scrapy,或者说默认查找的路径中找不到scrapy。
解决办法:通过file=>Default Settings=>Project Interpreter 重新关联python,pycharm的local中加入本地的python.exe
XPaths谷歌插件使用
插件下载地址: https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo?hl=zh-CN 安装完毕xpath之后,需要重新加载页面,方可使用。
scrapy 生成json文件中文是Unicode字符
错误分析:scrapy在做json序列化的时候默认使用的是ensure_ascii 编码。
解决办法:在设置当中(settings.py),加入FEED_EXPORT_ENCODING = 'utf-8'
。
解决爬取数据频繁时,被禁止
错误分析:网站本身有反爬虫,会针对频繁快速拉去的ip和用户进行封锁。 解决办法:设置动态的用会代理和动态的ip代理。 在中间件文件middlewares.py中,增加动态IP代理类和动态用户代理类。
代码语言:javascript复制import random
PROXIES = [
{'ip_port': '106.39.179.244:80'},
{'ip_port': '65.52.223.99:80'},
{'ip_port': '1.52.248.207:3128'},
{'ip_port': '45.77.198.207:3128'},
{'ip_port': '177.125.119.16:8080'},
{'ip_port': '174.138.65.233:3128'}
]
class RandomUserAgent(object):
"""Randomly rotate user agents based on a list of predefined ones"""
def __init__(self, agents):
self.agents = agents
@classmethod
def from_crawler(cls, crawler):
return cls(crawler.settings.getlist('USER_AGENTS'))
def process_request(self, request, spider):
# print "**************************" random.choice(self.agents)
request.headers.setdefault('User-Agent', random.choice(self.agents))
class ProxyMiddleware(object):
def process_request(self, request, spider):
proxy = random.choice(PROXIES)
print("**************ProxyMiddleware no pass************" proxy['ip_port'])
request.meta['proxy'] = "http://%s" % proxy['ip_port']
这组动态IP需要注意的是,隔一段时间可能会失效,需要重新获取IP。 在配置文件settings.py中增加
代码语言:javascript复制 DOWNLOADER_MIDDLEWARES = {
'tutorial.middlewares.RandomUserAgent': 1,
'scrapy.contrib.downloadermiddleware.httpproxy.HttpProxyMiddleware': 110,
'tutorial.middlewares.ProxyMiddleware': 100
}
USER_AGENTS = [
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
"Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
"Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
"Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
"Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527 (KHTML, like Gecko, Safari/419.3) Arora/0.6",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
]