Word Frequency Counter TypeError: 'Response' has no 'len'

0 Anantmurti Hegde · May 4, 2015
Hi, I'm trying to do this but getting an unknown error. Code and error are below:

import requests
from bs4 import BeautifulSoup
import operator


def start(url):
word_list = []
source_code = requests.get(url)
soup = BeautifulSoup(source_code)
for post_text in soup.findAll('a', {'class': 'post-title'}):
content = post_text.string
words = content.lower().split()
for each_word in words:
print(each_word)
word_list.append(each_word)

start("https://www.thenewboston.com/forum/category.php?id=15&orderby=recent&page=1")


Error:
Traceback (most recent call last):
  File "C:/Users/Anant/PycharmProjects/youtube/main.py", line 17, in <module>
    start("https://www.thenewboston.com/forum/category.php?id=15&orderby=recent&page=1")
  File "C:/Users/Anant/PycharmProjects/youtube/main.py", line 9, in start
    soup = BeautifulSoup(source_code)
  File "C:\Python34\lib\site-packages\bs4\__init__.py", line 162, in __init__
    elif len(markup) <= 256:
TypeError: object of type 'Response' has no len()

Process finished with exit code 1

Can someone please help me with this!

Post a Reply

Replies

Oldest  Newest  Rating
0 Bernard Pyc0d333 Parah · May 10, 2015
your code is ok, just one problem

soup = BeautifulSoup(source_code)


here you should rather pass the actual html as a param.



soup = BeautifulSoup(source_code.text)


here is my result after doing that:
python
help
extract
content
webpage

Pylove
  • 1

Python

107,107 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator