我想使用代理IP进行网页抓取。为了使用代理,我设置了文档中提到的环境变量http_proxy
。在
$ export http_proxy=http://proxy:port
为了测试IP更改是否有效,我创建了一个名为test的新spider:
from scrapy.spider import BaseSpider
from scrapy.contrib.spiders import CrawlSpider, Rule
class TestSpider(CrawlSpider):
name = "test"
domain_name = "whatismyip.com"
start_urls = ["http://whatismyip.com"]
def parse(self, response):
print response.body
open('check_ip.html', 'wb').write(response.body)
但是如果我运行这个spider,check_ip.html
不会显示环境变量中指定的IP,而是显示原始IP,就像爬行之前一样。在
有什么问题吗?有没有其他方法可以检查我是否使用代理IP?或者有没有其他方法可以使用代理IP?在
编辑设置.py在当前项目中,并确保启用了HttpProxyMiddleware:
相关问题 更多 >
编程相关推荐