我写了一个蜘蛛从我的IP http://ip.42.pl/raw通过PROXY.这是我的第一只蜘蛛.我想更改user_agent.我从本教程中获得了信息http://blog.privatenode.in/torifying-scrapy-project-on-ubuntu
我完成了本教程中的所有步骤,这是我的代码.
BOT_NAME = 'CheckIP'
SPIDER_MODULES = ['CheckIP.spiders']
NEWSPIDER_MODULE = 'CheckIP.spiders'
USER_AGENT_LIST = ['Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3',
'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
'Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
'Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9',
'Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; HTC_IncredibleS_S710e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'
]
HTTP_PROXY = 'http://127.0.0.1:8123'
DOWNLOADER_MIDDLEWARES = {
'CheckIP.middlewares.RandomUserAgentMiddleware': 400,
'CheckIP.middlewares.ProxyMiddleware': 410,
'CheckIP.contrib.downloadermiddleware.useragent.UserAgentMiddleware': None,
}
Run Code Online (Sandbox Code Playgroud)
import random
from scrapy.conf import settings
from scrapy import log
class RandomUserAgentMiddleware(object):
def process_request(self, request, spider):
ua = random.choice(settings.get('USER_AGENT_LIST'))
if ua:
request.headers.setdefault('User-Agent', ua)
#this is just to check which user agent is being used for request
spider.log(
u'User-Agent: {} {}'.format(request.headers.get('User-Agent'), request),
level=log.DEBUG
)
class ProxyMiddleware(object):
def process_request(self, request, spider):
request.meta['proxy'] = settings.get('HTTP_PROXY')
Run Code Online (Sandbox Code Playgroud)
import time
from scrapy.spider import Spider
from scrapy.http import Request
class CheckIpSpider(Spider):
name = 'checkip'
allowed_domains = ["ip.42.pl"]
url = "http://ip.42.pl/raw"
def start_requests(self):
yield Request(self.url, callback=self.parse)
def parse(self, response):
now = time.strftime("%c")
ip = now+"-"+response.body+"\n"
with open('ips.txt', 'a') as f:
f.write(ip)
Run Code Online (Sandbox Code Playgroud)
这是USER_AGENT的返回信息
2015-10-30 22:24:20+0200 [scrapy] DEBUG: Web service listening on 127.0.0.1:6080
2015-10-30 22:24:20+0200 [checkip] DEBUG: User-Agent: Scrapy/0.24.4 (+http://scrapy.org) <GET http://ip.42.pl/raw>
Run Code Online (Sandbox Code Playgroud)
用户代理:Scrapy/0.24.4(+ http://scrapy.org)
当我手动添加标头请求一切正常.
def start_requests(self):
yield Request(self.url, callback=self.parse, headers={"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3"})
Run Code Online (Sandbox Code Playgroud)
这是在控制台中返回的结果
2015-10-30 22:50:32+0200 [checkip] DEBUG: User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3 <GET http://ip.42.pl/raw>
Run Code Online (Sandbox Code Playgroud)
如何在我的蜘蛛中使用USER_AGENT_LIST?
如果你不需要一个随机的user_agent,你可以放置USER_AGENT你的设置文件,如:
settings.py:
...
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
...
Run Code Online (Sandbox Code Playgroud)
不需要中间件.但是如果你想真正随机选择一个user_agent,首先要确保RandomUserAgentMiddleware正在使用的scrapy日志,你应该在你的日志上检查这样的东西:
Enabled downloader middlewares:
[
...
'CheckIP.middlewares.RandomUserAgentMiddleware',
...
]
Run Code Online (Sandbox Code Playgroud)
检查那CheckIP.middlewares是中间件的路径.
现在可能在中间件上错误地加载了设置,我建议使用该from_crawler方法加载:
Class RandomUserAgentMiddleware(object):
def __init__(self, settings):
self.settings = settings
@classmethod
def from_crawler(cls, crawler):
settings = crawler.settings
o = cls(settings, crawler.stats)
return o
Run Code Online (Sandbox Code Playgroud)
现在self.settings.get('USER_AGENT_LIST')用于在process_request方法中获得你想要的东西.
也请更新您的scrapy版本,看起来就像你正在使用0.24,而它已经通过1.0.
| 归档时间: |
|
| 查看次数: |
9946 次 |
| 最近记录: |