擅长:python、mysql、java
<p>如果您打印<code>test_html</code>,您会发现得到403禁止响应。你知道吗</p>
<p>您应该向GET请求添加一个头(至少是一个用户代理:))。你知道吗</p>
<pre class="lang-py prettyprint-override"><code>import requests
from bs4 import BeautifulSoup
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'
}
url = 'https://www.brewersfriend.com/homebrew/recipe/view/16367/southern-tier-pumking-clone'
test_html = requests.get(url, headers=headers).text
soup = BeautifulSoup(test_html, 'html5lib')
rating = soup.find('span', {'itemprop': 'ratingValue'})
print(rating.text)
# 4.58
</code></pre>