beautiful soup TypeError: 'bytes' object is not callable

beautiful soup TypeError: 'bytes' object is not callable

我的代码

page = requests.get(URL).content()
soup = BeautifulSoup(page,"html.parser")
prices = soup.find("span", class_="value hidden-xs").encode
for row in prices('div',
                        attrs = {'class':'col-md-6 col-xs-12'}):
    price = {}

    price['url'] = row.span['data-value']

    prices.append(price)
print(prices)

输出:

line 6, in <module>
    page = requests.get(URL).content()
TypeError: 'bytes' object is not callable

这是我的第一个项目 python 和漂亮的汤所以如果你能解释它有什么问题我将不胜感激 我确实尝试了 typeerror 'bytes' object is not callable 中的解决方案,但我认为它不适用于 python 3

当我在没有 .content 的情况下尝试时,它给出了:

TypeError: object of type 'Response' has no len()

当我运行这段代码时:

page = requests.get(URL)
soup = BeautifulSoup(page,"html.parser")
prices = soup.find("span", class_="value hidden-xs").encode('utf-8')
print(prices)

输出:

b'<span class="value hidden-xs" data-value="1910.2">\xdb\xb1\xdb\xb9\xdb\xb1\xdb\xb0.\xdb\xb2</span>'

我需要收集这个“数据值”中的 9 个值并将它们放入一个数组中

page = requests.get(URL)
# ^^^^ this a response object
soup = BeautifulSoup(page,"html.parser")
prices = soup.find("span", class_="value hidden-xs").encode('utf-8')
print(prices)

当您使用 requests.get 函数时,您将返回一个 Response object。响应 object 具有各种属性,如状态代码、headers、url 和 textcontent 属性,其中包含字符串(文本)或字节(内容)。

在您的示例中,您应该使用:

response = requests.get(URL)
soup = BeautifulSoup(response.text,"html.parser")
prices = soup.find("span", class_="value hidden-xs").encode('utf-8')
print(prices)

有关在 Python 中使用 BeautifulSoup 的完整介绍,请参阅我的博客文章 Web Scraping with Python and BeautifulSoup