无法使用scrapy框架307重定向错误来刮取myntra API数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用scrapy框架307重定向错误来刮取myntra API数据相关的知识,希望对你有一定的参考价值。
下面是蜘蛛代码:
import scrapy
class MyntraSpider(scrapy.Spider):
custom_settings = {
'HTTPCACHE_ENABLED': False,
'dont_redirect': True,
#'handle_httpstatus_list' : [302,307],
#'CRAWLERA_ENABLED': False,
'USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
}
name = "heytest"
allowed_domains = ["www.myntra.com"]
start_urls = ["https://www.myntra.com/web/v2/search/data/duke"]
def parse(self, response):
self.logger.debug('Parsed jabong.com')
“Parsed jabong.com”未被记录。实际上,回调方法(parse)没有被调用。请恢复。
请从疤痕中心找到错误日志:
答案
我运行这段代码(只有几次),我没有问题来获取数据。
它看起来与你的代码类似,所以我不知道为什么你有问题。
也许他们出于某种原因阻止了你。
#!/usr/bin/env python3
import scrapy
import json
class MySpider(scrapy.Spider):
name = 'myspider'
allowed_domains = ['www.myntra.com']
start_urls = ['https://www.myntra.com/web/v2/search/data/duke']
#def start_requests(self):
# for tag in self.tags:
# for page in range(self.pages):
# url = self.url_template.format(tag, page)
# yield scrapy.Request(url)
def parse(self, response):
print('url:', response.url)
#print(response.body)
data = json.loads(response.body)
print('data.keys():', data.keys())
print('meta:', data['meta'])
print("data['data']:", data['data'].keys())
# download files
#for href in response.css('img::attr(href)').extract():
# url = response.urljoin(src)
# yield {'file_urls': [url]}
# download images and convert to JPG
#for src in response.css('img::attr(src)').extract():
# url = response.urljoin(src)
# yield {'image_urls': [url]}
# --- it runs without project and saves in `output.csv` ---
from scrapy.crawler import CrawlerProcess
c = CrawlerProcess({
'USER_AGENT': 'Mozilla/5.0',
#'USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
# save in CSV or JSON
'FEED_FORMAT': 'csv', # 'json
'FEED_URI': 'output.csv', # 'output.json
# download files to `FILES_STORE/full`
# it needs `yield {'file_urls': [url]}` in `parse()`
#'ITEM_PIPELINES': {'scrapy.pipelines.files.FilesPipeline': 1},
#'FILES_STORE': '/path/to/valid/dir',
# download images and convert to JPG
# it needs `yield {'image_urls': [url]}` in `parse()`
#'ITEM_PIPELINES': {'scrapy.pipelines.files.ImagesPipeline': 1},
#'IMAGES_STORE': '/path/to/valid/dir',
#'HTTPCACHE_ENABLED': False,
#'dont_redirect': True,
#'handle_httpstatus_list' : [302,307],
#'CRAWLERA_ENABLED': False,
})
c.crawl(MySpider)
c.start()
以上是关于无法使用scrapy框架307重定向错误来刮取myntra API数据的主要内容,如果未能解决你的问题,请参考以下文章