Python 使用meta从不更新的刮擦通过值
我当时正试图搜刮一些网站。我已经得到了数据,我尝试使用Python 使用meta从不更新的刮擦通过值,python,web-scraping,scrapy,python-requests,scrape,Python,Web Scraping,Scrapy,Python Requests,Scrape,我当时正试图搜刮一些网站。我已经得到了数据,我尝试使用meta={}传递值。但是当我使用yield scrapy.Request进入下一个函数时,问题就出现了。我被发送到下一个函数是一个新的URL,并使用meta传递JSON值。我得到了新的URL,但没有JSON数据,JSON从未更新过。只是传递了相同的值。我不知道发生了什么事 您可以看到我的代码,我试图传递JSON值,但是我只得到相同的JSON,但是URL用新的URL更新 def first_function(self, response):
meta={}
传递值。但是当我使用yield scrapy.Request
进入下一个函数时,问题就出现了。我被发送到下一个函数是一个新的URL,并使用meta
传递JSON值。我得到了新的URL,但没有JSON数据,JSON从未更新过。只是传递了相同的值。我不知道发生了什么事
您可以看到我的代码,我试图传递JSON值,但是我只得到相同的JSON,但是URL用新的URL更新
def first_function(self, response):
value_json = self.get_json() #i got the json from here
for key, value in value_json.items(): #loop the json
for values in value:
# values will show me as dictionay of dictionary
# thats like {"blabla":{"key1":"value1","key1":"value2"}}
# I gave the conditions as below to get just a value or to make sure if that key ("blabla") is exist
# if the condition is true, i will get the value {"key1":"value1","key1":"value2"}
if values == "blabla":
get_url = "http://www.example.com/"
yield Request(
url=get_url+values["id_url"],
meta={"data_rest":values},
callback=self.second_function
)
def second_function(self, response):
# ============== PROBLEM =====================
# The problem is here!
# I always got a new url from first_function, my logic is if I got a new url, i should get a new json
# but the json or "data_rest" never updated. Always send the same json to this function
# ============== PROBLEM =====================
josn_data = response.meta["data_rest"]
names = response.css() #get the tag in here
for get_data in names:
sub_url = get_data.css("a::attr(href)").extract()
for loop_url_menu in sub_url:
yield scrapy.Request(
url=loop_url_menu,
headers=self.session,
meta = {
'dont_redirect': True,
'handle_httpstatus_list': [302]
}, callback=self.next_function
)
好消息!!
我能解决它。我们只需要为第一个附加值,然后我们就可以得到值
def first_function(self, response):
temp = []
value_json = self.get_json() #i got the json from here
for key, value in value_json.items(): #loop the json
for values in value:
if values == "blabla":
get_url = "http://www.example.com/"
temp.append(values)
yield Request(
url=get_url+values["id_url"],
meta={"data_rest":temp},
callback=self.second_function
)
这可能与您仅在满足条件时才产生响应有关(
如果value==“blablabla”:
)因此meta总是{“data_rest”:“blabla”}
?我的意思是“blablabla”:{“data1”:“value 1”,“data2”:value 2”}.那是字典中的字典。如果我没有给出这样的条件。他们会给我钥匙,你能更新问题中的if条件吗?-从这个评论来看不是100%清楚。