python:scrapy使用代理IP

2024-06-23 20:02:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用代理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?在


Tags: namefromtestimportipcomhttp代理

热门问题