Python请求无法获取内容

时间:2021-02-22 08:02:06

标签: python python-3.x web-scraping python-requests

我创建了这个脚本来测试身份验证密钥并返回密钥是否有效。但是由于某种原因,如果脚本自动发送它,我会不断收到 404 错误

print('Select your key list')
keyfile = easygui.fileopenbox('', 'Select your combo file')
lines = len(open(keyfile).readlines())
while lines > 0:
    with open(keyfile, 'r') as fileforkeys:
         for key in fileforkeys:
         lines -= lines
         url = f'https://api.mullvad.net/www/accounts/{key}'
         r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'})
         response = str(r.content)
         print(r.status_code)
         if r.status_code == 200:
             print(Fore.GREEN + f'{key} works!')

         else:
             print(response)
             print(Fore.RED + f'{key} failed!')

当我运行此代码时,它会收到 404 错误和以下响应:

b'{"code":"ACCOUNT_NOT_FOUND"}'

密钥确实有效,所以这没有任何意义。如果我将脚本更改为此它的工作原理:

print('Select your key list')
keyfile = easygui.fileopenbox('', 'Select your combo file')
lines = len(open(keyfile).readlines())
while lines > 0:
    with open(keyfile, 'r') as fileforkeys:
         for key in fileforkeys:
         lines -= lines
         url = 'https://api.mullvad.net/www/accounts/0189605687294417' # actual working key is placed here which makes the script work.
         r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'})
         response = str(r.content)
         print(r.status_code)
         if r.status_code == 200:
             print(Fore.GREEN + f'{key} works!')

         else:
             print(response)
             print(Fore.RED + f'{key} failed!')

所以基本上,如果我将密钥插入到脚本中而不是让脚本迭代它可以工作的文本文件。这毫无意义,因为它最终会发送相同的 URL 和标头。为什么会发生这种情况?

0 个答案:

没有答案