【发布时间】:2022-02-06 03:57:14
【问题描述】:
我正在尝试使用 python 创建一个 Instagram 帖子下载机器人:
import requests
import re
#get url's detail
def get_response(url):
r = requests.get(url)
while r.status_code != 200:
r = requests.get(url)
return r.text
def prepare_urls(matches):
return list({match.replace("\\u0026", "&") for match in matches})
url = input('Enter Instagram URL: ')
response = get_response(url)
#check if there is video url or picture url in the json webpage that is opened
vid_matches = re.findall('"video_url":"([^"]+)"', response)
pic_matches = re.findall('"display_url":"([^"]+)"', response)
vid_urls = prepare_urls(vid_matches)
pic_urls = prepare_urls(pic_matches)
if vid_urls:
print('Detected Videos:\n{0}'.format('\n'.join(vid_urls)))
if pic_urls:
print('Detected Pictures:\n{0}'.format('\n'.join(pic_urls)))
if not (vid_urls or pic_urls):
print('Could not recognize the media in the provided URL.')
完成代码后,我通过视频链接进行了尝试,并且成功了。 1 小时后,我尝试了相同的视频链接,但它打印出第三个条件:“无法识别提供的 URL 中的媒体。”。
我很困惑 。如您所见,我从未在代码中使用过我的登录凭据,但第一次有效,第二次无效...
有什么想法吗?
【问题讨论】:
-
您的 get_response() 函数存在严重缺陷。如果 status_code 不是 200,您的代码将无限循环,希望服务器会改变主意。那只是一厢情愿。一些状态代码值得重试,但它们只是少数可能的响应