我第一次使用BeautifulSoup。我观看了YouTube视频并模仿了他们的所作所为。我试图从一个合适的网站上提取租金价格,但我不断得到这个消息:
Traceback (most recent call last):
File "C:\Users\Eun Hae Lee\AppData\Local\Programs\Python\Python35-32\rent.py", line 5, in <module>
sauce = urllib.request.urlopen("http://www.equityapartments.com/washington-dc/georgetown/2400-m-apartments##unit-availability-tile").read()
File "C:\Users\Eun Hae Lee\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Eun Hae Lee\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 471, in open
response = meth(req, response)
File "C:\Users\Eun Hae Lee\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Eun Hae Lee\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 509, in error
return self._call_chain(*args)
File "C:\Users\Eun Hae Lee\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File "C:\Users\Eun Hae Lee\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
此错误消息的原因是什么?
我使用的代码是
import urllib.request
import bs4 as bs
sauce = urllib.request.urlopen("http://www.equityapartments.com/washington-dc/georgetown/2400-m-apartments##unit-availability-tile").read()
soup = bs.beautifulsoup(sauce, 'html.parser')
print(soup)
感谢您的帮助!
答案 0 :(得分:0)
此错误与BeautifulSoup无关 - 您从远程服务器获取urlopen
操作中的400。您需要检查自己的网址,并确定其不合法的GET
。
答案 1 :(得分:0)
尝试
from bs4 import BeautifulSoup
import requests
url = "http://www.equityapartments.com/washington-dc/georgetown/2400-m-apartments##unit-availability-tile"
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "html.parser")
print (soup)