请帮我将referer_url添加到Request的Referer中,并在Request中使用referer: def parse(self, response): rs = json.loads(response.text) data = rs.get('items') for res_item in data: movie_item = newmovieItem() movie_item['movie_id'] = res_item.get('id') uri = res_item.get('uri') re_url = str(uri).replace("douban://douban.com/movie/", "https://movie.douban.com/subject/") referer_url = re_url + '/' detail_url = str(uri).replace("douban://douban.com/movie/", "https://www.douban.com/doubanapp/dispatch?uri=/movie/") print(detail_url) movie_item['url'] = detail_url yield Request(url=detail_url, callback=self.parse_detail, cb_kwargs={'item': movie_item}, dont_filter=True )
时间: 2024-02-14 13:03:04 浏览: 309
Referer-parser.cr:Snowplow的Referer-parser的Crystal实现
可以使用headers参数来添加Referer,并在Request中使用headers参数传递。修改后的代码如下:
```
def parse(self, response):
rs = json.loads(response.text)
data = rs.get('items')
for res_item in data:
movie_item = newmovieItem()
movie_item['movie_id'] = res_item.get('id')
uri = res_item.get('uri')
re_url = str(uri).replace("douban://douban.com/movie/", "https://movie.douban.com/subject/")
referer_url = re_url + '/'
detail_url = str(uri).replace("douban://douban.com/movie/", "https://www.douban.com/doubanapp/dispatch?uri=/movie/")
print(detail_url)
headers = {'Referer': referer_url}
yield Request(url=detail_url,
callback=self.parse_detail,
cb_kwargs={'item': movie_item},
dont_filter=True,
headers=headers
)
```
阅读全文