在Python中使用“requests”模块时出现错误405

e0bqpujr  于 2023-01-14  发布在  Python
关注(0)|答案(2)|浏览(348)

Update: Issue seems to be with Windows Powershell. Program works in Python IDLE.
So I have installed requests, urllib3 module properly. But whenever I try to use requests, I get HTTP 405 error. Please check the attached screenshot for my code and the error I get.

  • NOTE: I tried attaching images of my code and error but StackOverflow app gave me an error.
  • NOTE 2: I have tried GET method too but it doesn't work either, it throws the same HTTP 405 error.
    My code:
from bs4 import BeautifulSoup
import requests

file = requests.post("https://w3schools.com/python/demopage.htm")

soup = BeautifulSoup(file,"lxml");

print(soup.prettify())

Error I get:

Traceback (most recent call last): File "requestspractice.py", line 1, in import requests File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\site-packages\requests__init__.py", line 43, in import urllib3 File "C:\Users\Prasanna\Python1\urllib3.py", line 15, in resp = urllib.request.urlopen(req) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen return opener.open(url, data, timeout) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open response = meth(req, response) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error return self._call_chain(*args) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain result = func(*args) File "C:\Users\Prasanna\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 405: Method Not Allowed

5jdjgkvh

5jdjgkvh1#

我相信你想做的是GET页面,而不是POST任何东西。

file = requests.get("https://w3schools.com/python/demopage.htm")
ohfgkhjo

ohfgkhjo2#

您的网址错误,应该在末尾“html”,但您使用的是:"示例网站"https://w3schools.com/python/demopage.htm"

相关问题