Object of type response has no len ошибка

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's user avatar

Gino Mempin

24.6k28 gold badges92 silver badges128 bronze badges

asked Apr 19, 2016 at 5:16

Bryan's user avatar

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

Matvei Nazaruk's user avatar

0

Try to pass the HTML text directly

soup = BeautifulSoup(html.text)

answered Apr 19, 2016 at 5:21

Jorge's user avatar

JorgeJorge

1,1369 silver badges15 bronze badges

html.parser is used to ignore the warnings in the page:

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

Tomer Shetah's user avatar

Tomer Shetah

8,3837 gold badges26 silver badges35 bronze badges

answered Jan 10, 2021 at 12:23

Heba Allah. Hashim's user avatar

If you’re using requests.get('https://example.com') to get the HTML, you should use requests.get('https://example.com').text.

Artjom B.'s user avatar

Artjom B.

61k24 gold badges124 silver badges222 bronze badges

answered Oct 18, 2018 at 19:06

Moshe G's user avatar

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

Atul's user avatar

AtulAtul

1,44712 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

Ozcar Nguyen's user avatar

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

mamal's user avatar

mamalmamal

1,74118 silver badges14 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's user avatar

ush189

1,3326 gold badges22 silver badges30 bronze badges

answered Jul 29, 2020 at 14:10

ddddd's user avatar

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

BiswajitPaloi's user avatar

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

bizimunda's user avatar

bizimundabizimunda

7792 gold badges7 silver badges25 bronze badges

This error occurs when you try to parse HTML code using the BeautifulSoup constructor but pass a response object instead of the response’s content.

You can solve this error by accessing the Response object’s content using dot notation.

For example,

import requests 
from bs4 import BeautifulSoup

URL = "https://datahub.io/awesome/football"
page = requests.get(URL)

soup = BeautifulSoup(page.content, "html.parser")
print(soup)

This tutorial will go through the error and how to solve it with code examples.


Table of contents

  • TypeError: object of type ‘Response’ has no len()
  • Example
    • Solution
  • Summary

TypeError: object of type ‘Response’ has no len()

We raise a Python TypeError when attempting to perform an illegal operation for a specific type. In this case, the type is Response.

The part ‘has no len()‘ tells us the map object does not have a length, and therefore len() is an illegal operation for the Response object.

Retrieving the length of an object is only suitable for iterable objects, like a list or a string.

The len() method implicitly calls the dunder method __len__() , which returns a positive integer representing the length of the object on which it is called.

All iterable objects have __len__ as an attribute.

Let’s check if __len__ is in the list of attributes for the Response object.

import requests 
from bs4 import BeautifulSoup

URL = "https://datahub.io/awesome/football"
page = requests.get(URL)

print(type(page))
print('__len__' in dir(page))
<class 'requests.models.Response'>
False

We can see that __len__ is not present in the attributes of the Response object.

We can retrieve the content from the response object using dot notation. Dot notation requires putting a dot after the object followed by the attribute we want to access. response.content returns the content of the response in bytes. In Python, bytes is an iterable sequence with a length.

Let’s verify that response.content has __len__ in its list of attributes.

import requests
from bs4 import BeautifulSoup

URL = "https://datahub.io/awesome/football"

page = requests.get(URL)

content = page.content

print(type(content))

print('__len__' in dir(content))
<class 'bytes'>
True

We can see that __len__ is present in the attributes of the bytes object.

Example

Let’s look at an example of trying to parse HTML code using BeautifulSoup and Requests. First, we will import the requests module and BeautifulSoup.

import requests 
from bs4 import BeautifulSoup

Next, we will make a GET request to a web page and save the response as a response object.

URL = "https://datahub.io/awesome/air-pollution"
page = requests.get(URL)

Then we can parse the HTML code using the BeautifulSoup constructor. The first argument of the BeautifulSoup constructor is the response object from the GET request, and the second is the appropriate parser for the HTML content.

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

Let’s run the code to see the result:

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

The error occurs because the BeautifulSoup constructor requires the response content, not the entire response.

Solution

We can solve the error by retrieving the response content using .content after the response object name.

It is preferable to use .content instead of .text as Requests guesses the text encoding for the response based on the HTTP headers.

Let’s look at the revised code:

import requests 
from bs4 import BeautifulSoup

URL = "https://datahub.io/awesome/air-pollution"
page = requests.get(URL)

soup = BeautifulSoup(page.content, "html.parser")
print(soup)

Let’s run the code to get the result:

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8"/>
<title>
    Air Pollution Data - Awesome Datasets
    - DataHub - Frictionless Data
  </title>
....

We successfully parsed the HTML content using the BeautifulSoup constructor.

We can also use .text, for example:

import requests 
from bs4 import BeautifulSoup

URL = "https://datahub.io/awesome/air-pollution"
page = requests.get(URL)

soup = BeautifulSoup(page.text, "html.parser")
print(soup)

Summary

Congratulations on reading to the end of this tutorial!

For further reading on the has no len() TypeErrors, go to the article:

  • How to Solve Python TypeError: object of type ‘zip’ has no len()
  • How to Solve Python TypeError: object of type ‘builtin_function_or_method’ has no len()
  • How to Solve Python TypeError: object of type ‘generator’ has no len()

To learn more about Python for data science and machine learning, go to the online courses page on Python, which provides the best, easy-to-use online courses.

Encountering the error «Object of Type ‘Response’ Has No len()» can be frustrating for developers. This comprehensive guide will help you understand the root cause of the error, provide step-by-step solutions, and answer common questions related to this error.

Table of Contents

  1. Understanding the Error
  2. Step-by-Step Solutions
  3. FAQs
  4. Related Links

Understanding the Error

The error «Object of Type ‘Response’ Has No len()» occurs primarily when working with APIs in Python, and it is usually encountered when using the requests library. This error pops up when you try to get the length of a Response object using the len() function. However, the Response object doesn’t have a __len__() method, which is why the error occurs.

Step-by-Step Solutions

Here are the steps to troubleshoot and resolve the «Object of Type ‘Response’ Has No len()» error:

Step 1: Identify the problematic line of code

The first step is to identify the line of code where the error occurs. Look for the line where you’re using the len() function on a Response object. It might look something like this:

response = requests.get('https://api.example.com/data')
response_length = len(response)

Step 2: Access the content of the Response object

Instead of using the len() function directly on the Response object, you need to access its content first. You can do this using the .content attribute of the Response object.

response_content = response.content

Step 3: Get the length of the content

Now that you have the content of the Response object, you can use the len() function to get its length.

response_length = len(response_content)

Step 4: Update the original code

Finally, update the original code with the changes from Steps 2 and 3. The updated code should look like this:

response = requests.get('https://api.example.com/data')
response_content = response.content
response_length = len(response_content)

FAQs

1. Can I use the len() function on the Response object’s .text attribute?

Yes, you can use the len() function on the Response object’s .text attribute, which returns the content as a string. However, note that .text and .content may produce different lengths if the content is not a string.

response_length = len(response.text)

2. What is the difference between the .content and .text attributes of the Response object?

The .content attribute returns the content as bytes, while the .text attribute returns the content as a string. If the content is a string, you can use either attribute to get the length.

3. Can I use the len() function on the .json() method of the Response object?

Yes, you can use the len() function on the result of the .json() method if the JSON data is a list or a dictionary. However, you might need to handle exceptions if the JSON data is not a list or a dictionary.

response_json = response.json()
response_length = len(response_json)

4. How can I handle exceptions when using the len() function on the .json() method result?

You can use a try-except block to handle exceptions, like this:

try:
    response_json = response.json()
    response_length = len(response_json)
except TypeError:
    print("Cannot get the length of the JSON data.")

5. How can I ensure that the Response object has a __len__() method?

One way to add a __len__() method to the Response object is by subclassing the requests.Response class and adding a __len__() method to the subclass. However, this approach is not recommended as it may introduce unexpected issues and is not necessary for most use cases.

  1. Requests Library Documentation
  2. Python Exceptions
  3. Working with JSON Data in Python

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]

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's user avatar

Gino Mempin

22.8k27 gold badges91 silver badges119 bronze badges

asked Apr 19, 2016 at 5:16

Bryan's user avatar

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

Matvei Nazaruk's user avatar

0

Try to pass the HTML text directly

soup = BeautifulSoup(html.text)

answered Apr 19, 2016 at 5:21

Jorge's user avatar

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's user avatar

Tomer Shetah

8,2637 gold badges24 silver badges35 bronze badges

answered Jan 10, 2021 at 12:23

Heba Allah. Hashim's user avatar

If you’re using requests.get('https://example.com') to get the HTML, you should use requests.get('https://example.com').text.

Artjom B.'s user avatar

Artjom B.

60.7k24 gold badges124 silver badges221 bronze badges

answered Oct 18, 2018 at 19:06

Moshe G's user avatar

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

Atul's user avatar

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

Ozcar Nguyen's user avatar

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

mamal's user avatar

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's user avatar

ush189

1,2566 gold badges21 silver badges29 bronze badges

answered Jul 29, 2020 at 14:10

ddddd's user avatar

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

BiswajitPaloi's user avatar

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

bizimunda's user avatar

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

Понравилась статья? Поделить с друзьями:
  • Object has no attribute python ошибка
  • Object cannot be created on a hidden layer ошибка
  • Obdmax диагностика и расшифровка кодов ошибок скачать
  • Obd2 коды ошибок нива шевроле
  • Obd2 как сбросить ошибки на