简体   繁体   中英

Error 405 when using "requests" module in Python

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_chai n 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

我相信你想要做的是GET的页面,而不是POST什么吧。

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

Your URL is wrong should be at the end "html", but you're using: "https://w3schools.com/python/demopage.htm"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM