发生错误的代码:
import requests
from bs4 import BeautifulSoup
url="https://www.baidu.com/"
def gethtmltext(url):
try:
r=requests.get(url)
if r.status_code==200:
r.encoding=r.apparent_encoding
return r.text
except requests.exceptions.RequestException:
return None
soup=BeautifulSoup(url,'html.parser')
print(soup)
错误:
looks like a URL. Beautiful Soup is not an HTTP client. You should probably use an HTTP client like requests to get the document behind the URL, and feed that document to Beautiful Soup.
' that document to Beautiful Soup.' % decoded_markup
解决的方法:
import requests
from bs4 import BeautifulSoup
url="https://www.baidu.com/"
def gethtmltext(url):
try:
r=requests.get(url)
if r.status_code==200:
r.encoding=r.apparent_encoding
return r.text
except requests.exceptions.RequestException:
return None
soup=BeautifulSoup(gethtmltext(url),'html.parser')
print(soup)