Python进阶之Scrapy通过下载中间件携带随机U-A
- 需求:
- Scrapy访问网页时采用随机U-A
- 测试页面: http://httpbin.org/user-agent
- 通过DOWNLOADER_MIDDLEWARES实现
Scrapy随机U-A项目
scrapy startproject MV
cd MV
scrapy genspider ua httpbin.org
ua.py
import scrapy
class UaSpider(scrapy.Spider):
name = 'ua'
allowed_domains = ['httpbin.org']
start_urls = ['http://httpbin.org/user-agent']
def parse(self, response):
print(response.text)
middlewares.py
- 在原有代码的下方添加类