使用BeautifulSoup时出现Python回溯错误

使用BeautifulSoup时出现Python回溯错误,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,我知道这个问题已经被问了一百次了,但我还没有找到一个对我的情况有意义的答案 我是python新手,尝试使用以下代码: import urllib import requests from bs4 import BeautifulSoup theurl = "https://twitter.com" thepage = requests.get(theurl) soup = BeautifulSoup(thepage, "html.parser") print(soup.title) 因此

我知道这个问题已经被问了一百次了,但我还没有找到一个对我的情况有意义的答案

我是python新手,尝试使用以下代码:

import urllib
import requests

from bs4 import BeautifulSoup

theurl = "https://twitter.com"
thepage = requests.get(theurl)
soup = BeautifulSoup(thepage, "html.parser")

print(soup.title)
因此,我得到以下错误:

Traceback (most recent call last):   File
"/Users/username/PycharmProjects/WebScraper2.0/web.py", line 8, in
 <module>
     soup = BeautifulSoup(thepage, "html.parser")   File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/__init__.py",
 line 192, in __init__
     elif len(markup) <= 256 and ( TypeError: object of type 'Response' has no len()
回溯(最近一次调用上次):文件
“/Users/username/PycharmProjects/WebScraper2.0/web.py”,第8行,在
soup=BeautifulSoup(页面,“html.parser”)文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/__-init___;.py”,
第192行,在_init中__

elif len(markup)您需要围绕您获取的URL文本调用
BeautifulSoup()
,而不是实际请求:

soup = BeautifulSoup(thepage.text, "html.parser")
请尝试以下代码段:

import requests
from bs4 import BeautifulSoup

r=requests.get("https://twitter.com")
c=r.content

soup=BeautifulSoup(c,"html.parser")

print(soup.title)

这与此有关吗:这就成功了!非常感谢。它还不允许我接受答案,但一旦它接受了,我会给你评分。
页面内容也应该有效,但不确定每个页面的详细差异。上面的代码片段是一个有效的演示,它给出了预期的结果。我找不到否决该解决方案的原因。