POST语句Python请求

问题描述 投票:0回答:1

由于错误403,我遇到了连接问题,这就是网站认为我是Bot的原因。我试图在POST语句中包含Header,但是做错了。感谢您的提前帮助:)

代码:

from requests import HTTPError
import requests
from bs4 import BeautifulSoup as bs

with requests.Session() as s:
   user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
   headers = {'User-Agent': user_agent}
   site = s.get("https://www.instagram.com")
   try:
      site = s.get("https://www.instagram.com", headers=headers)
      site.raise_for_status()
   except HTTPError as http_err:
      print(f'HTTP error occurred: {http_err}')  
   except Exception as err:
      print(f'Other error occurred: {err}')  
   else:
      print('Successfully opened Webpage!')

   bs_content = bs(site.content, "html.parser")
   login_data = {"username":"test","password":"test"}

   try:
      response = s.post("https://www.instagram.com",login_data, None, headers)
      response.raise_for_status()
   except HTTPError as http_err:
      print(f'HTTP error occurred: {http_err}')  
   except Exception as err:
      print(f'Other error occurred: {err}')  
   else:
      print('Successfully logged in!')

   home_page = s.get("https://www.instagram.com/instagram/")
python html python-3.x python-requests http-status-code-403
1个回答
0
投票

您应该尝试使用Instagram API,因为常规的UI通常具有阻止漫游器的功能。

请参见https://www.instagram.com/developer/endpoints/

© www.soinside.com 2019 - 2024. All rights reserved.