使用 BeautifulSoup 常見得錯誤訊息 object of type 'Response' has no len()

object of type 'Response' has no len()

# 上述的錯誤訊息是新手使用 BeautifulSoup 常遇見的錯誤 
# 最主要的原因是忘了加 content 的方法

# 解決方法如下
import requests

headers = {
    'content-type': 'text/html; charset=UTF-8',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'
}

url = 'https://python-learnnotebook.blogspot.com/'
response = requests.get(url=url,headers=headers,timeout=15)
html = response.content # host return result use content method can slove your issue
soup = BeautifulSoup(html, 'html.parser')

沒有留言

技術提供:Blogger.