Mskouyoudjian
Mskouyoudjian

Reputation: 1

Beautiful Soup Error: object of type 'method' has no len

Scripting a web scraper. Traceback shows beautiful soup line of code as the error

I have tried using previous versions of beautiful soup, running it in Sublime and in command console

my_url = 'http://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?            


uClient = uReq(my_url)
page_html = uClient.read
uClient.close

page_soup = Soup(page_html, "html.parser")

Traceback (most recent call last):
  File "C:\Users\mskou\OneDrive\Desktop\Sublime Text Scripts\First Sublime Web Scrape.py", line 14, in <module>
    page_soup = Soup(page_html, "html.parser")
  File "C:\Users\mskou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\bs4\__init__.py", line 267, in __init__
    elif len(markup) <= 256 and (
TypeError: object of type 'method' has no len()

Upvotes: 0

Views: 1352

Answers (2)

AllJs
AllJs

Reputation: 1810

A Cleaner version if you are using the Requests library.

from bs4 import BeautifulSoup as Soup
import requests 


headers_Get = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
    'Content-Type': 'application/json'
}

my_url = 'http://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?'

r = requests.get(my_url, headers=headers_Get)             

soup = BeautifulSoup(r.content, 'html.parser') 

print(soup.prettify())

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195508

You are missing parentheses () in your method calls:

import urllib.request
from bs4 import BeautifulSoup as Soup

my_url = 'http://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?'


uClient = urllib.request.urlopen(my_url)
page_html = uClient.read()  # <--- put () here
uClient.close()   # <--- ...and here
page_soup = Soup(page_html, "html.parser")

print(page_soup.prettify())

Upvotes: 1

Related Questions