Home>
The code does not work on the server, although it works on a computer.
import requests
POSTFIX= '? __ a= 1'
HEADERS= {
'accept': 'text /html, application /xhtml + xml, application /xml; q= 0.9, image /avif, image /webp, image /apng, * /*; q= 0.8, application /signed-exchange; v= b3; q= 0.9 ',
'user-agent': 'Mozilla /5.0 (X11; Linux x86_64) AppleWebKit /537.36 (KHTML, like Gecko) Chrome /88.0.4324.96 Safari /537.36'
}
def get_json (url):
page= requests.get (url, headers= HEADERS)
json= page.json ()
return json
def get_images_link (path):
images_link= path
return images_link
def get_videos_link (path):
videos_link= path
return videos_link
def calculate_the_type (path):
if path ['__ typename']== 'GraphImage':
return 'photo'
else:
return 'video'
def get_links (url):
if url.endswith ('? utm_source= ig_web_copy_link'):
url= url [: url.find ('? utm_source= ig_web_copy_link')]
url= url [: url.find ("? igshid=")]
json= get_json (url + POSTFIX)
links= {}
try:
path= json ['graphql'] ['shortcode_media'] ['edge_sidecar_to_children'] ['edges']
number_of_objects= 0
for obj in path:
number_of_objects += 1
for i in range (number_of_objects):
if calculate_the_type (path [i] ['node'])== 'photo':
links [get_images_link (path [i] ['node'] ['display_url'])]= 'photo'
else:
links [(get_videos_link (path [i] ['node'] ['video_url']))]= 'video'
except KeyError:
path= json ['graphql'] ['shortcode_media']
if calculate_the_type (path)== 'photo':
links [get_images_link (path ['display_url'])]= 'photo'
else:
links [get_videos_link (path ['video_url'])]= 'video'
return links
if __name__== '__main__':
link= str (input ('Enter a link to the object:'))
urls= get_links (link)
for key in urls:
print (key, urls [key])
Error text
Traceback (most recent call last):
File "test.py", line 60, in <
module >
urls= get_links (link)
File "test.py", line 36, in get_links
json= get_json (url + POSTFIX)
File "test.py", line 12, in get_json
json= page.json ()
File "/code/photo_taker/venv/lib/python3.8/site-packages/requests/models.py", line 900, in json
return complexjson.loads (self.text, ** kwargs)
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode (s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end= self.raw_decode (s, idx= _w (s, 0). end ())
File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError ("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
everything is fine with url, but for some reason, json is not parsed on the server
Рауль Алимбеков2021-02-23 17:23:27"everything is fine with url" ... ok, but is there something in the page, preferably json?
Jack_oS2021-02-23 17:30:46only get_json does not work on the server, similarly everything is fine on my computer, I tried the json module too, but the result is the same
Рауль Алимбеков2021-02-23 17:41:05some code on heroku?
Jack_oS2021-02-23 17:49:01Related questions
- python : Problem sending message to json data server
- python : How to send in a POST request JSON with the character set $, \ n, []
- python : Json decoder not working on server
- python : Getting street name from json response api yandex geocoder
- python : How do I write an assert on one of the objects in a json array?
- python : requests.exceptions.TooManyRedirects: Exceeded 30 redirects
- python : 500 Error problem
- python : How to write cookies to the database so that they can be used later?
- python : JSON file encoding
check what is passed to def get_json (url): as url? and what's in page in the same function? .. json.decoder.JSONDecodeError -json is not parsed
Jack_oS2021-02-23 15:13:47