When I try to execute the code
BeautifulSoup(html, ...)
it gives the error message
TypeError: object of type ‘Response’ has no len()
I tried passing the actual HTML as a parameter, but it still doesn’t work.
import requests
url = 'http://vineoftheday.com/?order_by=rating'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, "html.parser")
Gino Mempin
22.8k27 gold badges91 silver badges119 bronze badges
asked Apr 19, 2016 at 5:16
2
You are getting response.content. But it return response body as bytes (docs). But you should pass str to BeautifulSoup constructor (docs). So you need to use the response.text instead of getting content.
answered Apr 19, 2016 at 5:25
0
Try to pass the HTML text directly
soup = BeautifulSoup(html.text)
answered Apr 19, 2016 at 5:21
![]()
JorgeJorge
1,1269 silver badges15 bronze badges
html.parser is used to ignore the warnings in the page:
soup = BeautifulSoup(html.text, "html.parser")
Tomer Shetah
8,2637 gold badges24 silver badges35 bronze badges
answered Jan 10, 2021 at 12:23
![]()
If you’re using requests.get('https://example.com') to get the HTML, you should use requests.get('https://example.com').text.
Artjom B.
60.7k24 gold badges124 silver badges221 bronze badges
answered Oct 18, 2018 at 19:06
![]()
Moshe GMoshe G
4661 gold badge4 silver badges13 bronze badges
you are getting only response code in ‘response’
and always use browser header for security otherwise
you will face many issues
Find header in debugger console network section ‘header’ UserAgent
Try
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
url = 'http://www.google.com'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
response = requests.get(quote_page, headers=headers).text
soup = BeautifulSoup(response, 'html.parser')
print(soup.prettify())
answered Jan 6, 2019 at 9:21
AtulAtul
1,39912 silver badges9 bronze badges
It worked for me:
soup = BeautifulSoup(requests.get("your_url").text)
Now, this code below is better (with lxml parser):
import requests
from bs4 import BeautifulSoup
soup = BeautifulSoup(requests.get("your_url").text, 'lxml')
answered Apr 9, 2019 at 5:08
you should use .text to get content of response
import requests
url = 'http://www ... '
response = requests.get(url)
print(response.text)
or use with soap
import requests
from bs4 import BeautifulSoup
url = 'http://www ... '
response = requests.get(url)
msg = response.text
print(BeautifulSoup(msg,'html.parser'))
answered Jul 12, 2020 at 21:24
![]()
mamalmamal
1,59116 silver badges13 bronze badges
import requests
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
url = "https://fortnitetracker.com/profile/all/DakshRungta123"
html = requests.get(url)
soup = BeautifulSoup(html)
title = soup.text
print(title.text)
![]()
ush189
1,2566 gold badges21 silver badges29 bronze badges
answered Jul 29, 2020 at 14:10
1
import requests
url = 'http://vineoftheday.com/?order_by=rating'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html.text, "html.parser")
answered Jun 17, 2022 at 11:58
3
from bs4 import BeautifulSoup
import requests
url = 'your_url'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, "html.parser")
print(soup)
answered Dec 14, 2022 at 10:02
bizimundabizimunda
6892 gold badges7 silver badges24 bronze badges
The python error TypeError: object of type ‘type’ has no len() occurs while attempting to find the length of an object that returns ‘type’. The python variables can store object data types. If a python variable refers a data type, it can’t be used in length function. The Length function is used for data structures that store multiple objects. The data type of the variable that store another data type is called ‘type’.
For example list is a data type, list() returns a object of type list. int is a data type, where as int() returns int value. Python supports to store a data type and value in a variable.
a = int -- stores data type
a = int() -- stores a int value
The python variables can store the data types and classes. These variables are not assigned any value, or objects. The length function can not be called the variables which are not allocated with any value or object. If you call the length function for these variables of ‘type’, it will throw the error TypeError: object of type ‘type’ has no len()
The basic data types are int, float, long, bool, complex etc. Some of the collections types are list, tuple, set, dict etc. Python allows to store these data types in a variable. The data type of the variable that store another data type is called ‘type’.
Exception
The error TypeError: object of type ‘type’ has no len() will show the stack trace as below
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print len(s)
TypeError: object of type 'type' has no len()
[Finished in 0.1s with exit code 1]
How to reproduce this issue
If a python variable is assigned with a data type, the type of the variable would have ‘type’. If the length function is invoked for the ‘type’ variable, the error TypeError: object of type ‘type’ has no len() will be thrown.
s=list
print len(s)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print len(s)
TypeError: object of type 'type' has no len()
[Finished in 0.1s with exit code 1]
Root Cause
The python variables are used to store any object or value. The values of the primitive data type such as int, float, long, boot etc can be stored in any python variable. Python is an object oriented programming language. The objects can be stored in Python variables. As the python variables are not declared with any data types, python can support to store a python function and a data type.
If a data type (not a value or object) is stored with a python variable, the length function can not be used for such variable. So, the error TypeError: object of type ‘type’ has no len() is thrown
Solution 1
The python variable which is assigned with a data type, should be assigned with a value or object. If the variable is not assigned with any value or object , assign with an object such as list, tuple, set, dictionary etc.
list – is a data type, where as list() is an object of type list
s=list()
print len(s)
Output
0
[Finished in 0.1s]
Solution 2
Due to the dynamic creation of the variable the python variable may not assigned with data types. The datatype of the variable is ‘type’. In this case, the data type must be checked before the length function is called.
s=list
print type(s)
if s is list :
print "Value is type"
else:
print (len(s))
Output
<type 'type'>
Value is type
[Finished in 0.0s]
Solution 3
The python variable should be validated for the data type. The length function is used to find the number of items in the objects. Before calling the length function, the object must be validated for the collection of items.
s=list
print type(s)
if type(s) in (list,tuple,dict, str):
print (len(s))
else:
print "not a list"
Output
<type 'type'>
not a list
[Finished in 0.1s]
Solution 4
The try and except block is used to capture the unusual run time errors. If the python variable contains expected value, then it will execute in the try block. If the python variable contains the unexpected value, then the except block will handle the error.
s=type
print s
try :
print (len(s))
except :
print "Not a list"
Output
<type 'type'>
Not a list
[Finished in 0.0s]
Вопрос:
Я знаю, что вариации этого вопроса задавались сто раз, но я не смог найти ответ, который имеет смысл для моей ситуации.
Я новичок в 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()
В чем проблема? Я все еще пытаюсь ознакомиться с кодами ошибок, и этот из того, что я могу сказать, кажется довольно общим. Кто-нибудь хочет помочь мне и объяснить, в чем проблема? Из примеров, которые я видел, это должно работать… что мне не хватает?
Лучший ответ:
Вы должны вызвать BeautifulSoup() вокруг текста URL-адреса, который вы захватите, а не фактического запроса:
soup = BeautifulSoup(thepage.text, "html.parser")
Ответ №1
Попробуйте ниже фрагмент:
import requests
from bs4 import BeautifulSoup
r=requests.get("https://twitter.com")
c=r.content
soup=BeautifulSoup(c,"html.parser")
print(soup.title)
Я парсю этот ресурс.Беру заголовок,дату и контент новости.
Выходит следующие сообщение об ошибке:
Traceback (most recent call last): File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 130, in <module> call_all_func(resources) File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 112, in call_all_func item_title = get_item_title(item_page,title_rule) File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 35, in get_item_title soup = BeautifulSoup(item_page, 'lxml') File "C:UsersАдминистраторAppDataLocalProgramsPythonPython37-32libsite-packagesbs4__init__.py", line 267, in __init__ elif len(markup) <= 256 and ( TypeError: object of type 'NoneType' has no len() Process finished with exit code 1
Как я понял из ошибки,ошибка содержится в этом участке кода:
# < Собираем заголовки с страницы. def get_item_title(item_page,title_rule): soup = BeautifulSoup(item_page, 'lxml') item_title = soup.find(title_rule[0],{title_rule[1]:title_rule[2]}) print(item_title) return item_title['content']
В title_rule=meta , title_rule=property , title_rule=og:title
Я решил сделать print(item_title) На выводе:
vesti кол-во ссылок: 34 http://vesti.kz//khl/269571/ <meta content='Прямая трансляция первого матча "Барыса" в новом сезоне КХЛ' property="og:title"/> http://vesti.kz//profi/269572/ <meta content='"Я вернусь в Украину чемпионом мира". Деревянченко сделал очередное заявление перед боем с Головкиным' property="og:title"/> http://vesti.kz//mirfutbol/269569/ <meta content="Криштиану Роналду составил завещание" property="og:title"/> http://vesti.kz//wta/269568/ <meta content="Путинцева показала лучший результат в карьере и завершила выступление на US Open " property="og:title"/> http://vesti.kz//mirfutbol/269566/ <meta content="Месси получил приз фонда Папы Римского " property="og:title"/> http://vesti.kz//amateur/269565/ <meta content="Казахстанский боксер рассказал о подготовке к ЧМ и включил Узбекистан в число главных соперников" property="og:title"/> http://vesti.kz//national/269564/ <meta content="Сборная Казахстана начала подготовку к матчам с Кипром и Россией в отборе на Евро-2020" property="og:title"/> http://vesti.kz//sportsout/269563/ <meta content="Китайский миллиардер из рейтинга Forbes передал Головкину эстафету в челлендже" property="og:title"/> http://vesti.kz//uefa/269509/ <meta content='Кто из звезд "Манчестер Юнайтед" может приехать в Казахстан на матч с "Астаной" в Лиге Европы' property="og:title"/> http://vesti.kz//france/269562/ <meta content="ПСЖ подписал трехкратного победителя Лиги чемпионов и отдал в аренду чемпиона мира" property="og:title"/> http://vesti.kz//amateur/269561/ <meta content="Василий Левит оценил свою форму и озвучил задачу на ЧМ-2019 по боксу " property="og:title"/> http://vesti.kz//mirfutbol/269560/ <meta content="ФИФА назвала трех претендентов на награду лучшему футболисту года" property="og:title"/> http://vesti.kz//profi/269547/ Traceback (most recent call last): File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 130, in <module> call_all_func(resources) File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 112, in call_all_func item_title = get_item_title(item_page,title_rule) File "C:/Users/Администратор/PycharmProjects/Task/parser.py", line 35, in get_item_title soup = BeautifulSoup(item_page, 'lxml') File "C:UsersАдминистраторAppDataLocalProgramsPythonPython37-32libsite-packagesbs4__init__.py", line 267, in __init__ elif len(markup) <= 256 and ( TypeError: object of type 'NoneType' has no len() Process finished with exit code 1
После вывода (если я правильно понял) код ругается на эту ссылку.
Как решить эту ошибку? По ошибке мне понятно только
что объект типа ‘NoneType’ не имеет len ()
Отредактировано r4khic (Сен. 3, 2019 08:16:03)
I was building a very simple price tracker and while attempting to get the price of an item from an Amazon listing, this happened.
Here is the code:
def get_price_from_url(self, url):
page = requests.get(url)
html = bs4.BeautifulSoup(page, 'html.parser')
price_element = html.find('span', {'id': priceblock_ourprice})[0] return price_element.text
And this is the error I get:
Traceback (most recent call last):
File «c:/Users/Franklin Joe/Documents/Projects/python_practice/Python Automation/Price Tracker/price_tracker.py», line 52, in <module>
main()
File «c:/Users/Franklin Joe/Documents/Projects/python_practice/Python Automation/Price Tracker/price_tracker.py», line 48, in main
test = get_price(‘https://www.amazon.in/GeForce-192-bit-Graphics-IceStorm-ZT-T20600F-10M/dp/B07MBKKQPW’, None, None)
File «c:/Users/Franklin Joe/Documents/Projects/python_practice/Python Automation/Price Tracker/price_tracker.py», line 12, in __init__
print(self.get_price_from_url(self.url))
File «c:/Users/Franklin Joe/Documents/Projects/python_practice/Python Automation/Price Tracker/price_tracker.py», line 41, in get_price_from_url
html = bs4.BeautifulSoup(page, ‘html.parser’)
File «C:UsersFranklin JoeAppDataLocalProgramsPythonPython37libsite-packagesbs4__init__.py», line 310, in __init__
elif len(markup) <= 256 and (
TypeError: object of type ‘Response’ has no len()
Я знаю, что варианты этого вопроса задавались сто раз, но я не смог найти ответ, который имеет смысл для моей ситуации.
Я новичок в 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()
В чем здесь проблема? Я все еще пытаюсь ознакомиться с кодами ошибок, и этот из того, что я могу сказать, кажется довольно общим. Кто-нибудь хочет помочь мне и объяснить, в чем проблема? Из примеров, которые я видел, это должно работать … что мне не хватает?
2 ответа
Лучший ответ
Вам нужно вызвать BeautifulSoup() вокруг текста URL, который вы захватили, а не фактический запрос:
soup = BeautifulSoup(thepage.text, "html.parser")
3
TerryA
14 Янв 2018 в 06:30
Попробуйте фрагмент ниже:
import requests
from bs4 import BeautifulSoup
r=requests.get("https://twitter.com")
c=r.content
soup=BeautifulSoup(c,"html.parser")
print(soup.title)
0
DeadCoderz
14 Янв 2018 в 06:37