语句后Python请求

2024-09-27 07:28:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有连接问题,由于错误403,这就是为什么网站认为我是一个机器人。我试图将我的标题包含在POST声明中,但我做错了什么。提前感谢您的帮助:)

代码:

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/")

Tags: httpsimportcomhttpaswwwsiteerror

热门问题