Very basic website -- scraper "'str' object is not callable"

  • Python
  • Thread starter Greg Bernhardt
  • Start date
In summary, Google may have blocked you, try again in an hour when you query the plugin for WordPress. If you are using a Mac, you may have an issue with the desktop user-agent. If you are using a mobile device, you may have an issue with the mobile user-agent.
  • #1
19,442
10,021
TL;DR Summary
Making a very basic web scraper and I have the error line 48 'str' object is not callable, and I can't figure it out.
Learning Python and this is a first attempt at a project.

Python:
import urllib
import requests
from bs4 import BeautifulSoup, Comment

# desktop user-agent
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"
# mobile user-agent
MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36"

getkeyword = "plugin"
query = "wordpress plugin"
query = query.replace(' ', '+')
URL = f"https://google.com/search?q={query}"

headers = {"user-agent": USER_AGENT}
resp = requests.get(URL, headers=headers)

if resp.status_code == 200:
    soup = BeautifulSoup(resp.content, "html.parser")
    results = []
    links= []
    for g in soup.find_all('div', class_='r'):
        anchors = g.find_all('a')
        if anchors:
            links.append(anchors[0]['href'])
else:
  print("Google may have blocked you, try again in an hour")

i=0
for num in links:  
  URL = links[i]
  resp = requests.get(URL, headers=headers)

  if resp.status_code == 200:
    soup = BeautifulSoup(resp.content, "html.parser")

    if soup.find('title'):
      title = soup.find('title').text

    if soup.find('h1'):
      h1 = soup.find('h1').text

    if soup.find('h2'):
      h2 = soup.find('h2').text

    bodytext = soup.find('body').text
   
    print("#" + str(i+1) + ": " + links[i])

    if title:
      print("Title: " + title)
    if h1:
      print("H1: " + h1)
    if h2:
      print("H2: " + h2)
   
    if getkeyword in bodytext:
      print = ("Keyword: Yes")
    else:
      print = ("Keyword: No")

    for comments in soup.findAll(text=lambda text:isinstance(text, Comment)):
      getComments = comments.extract()
      if "Yoast" in getComments:
        print("Yoast: Yes")
        break
    print("\r\r")
  else:
    print("Site is unavailable")
    break

  i += 1
 
del links
 
  • Like
Likes sysprog and atyy
Technology news on Phys.org
  • #2
A bit of Googling suggests that this can happen if you've defined a variable called str before running your script. Then python thinks you are referring to this variable and wonders why you are trying to call a variable. Try adding del str to the top of your script and running it. If that works, you should be able to remove the line you just added.
 
  • Like
Likes sysprog
  • #3
Ibix said:
Try adding del str to the top of your script and running it.
NameError: name 'str' is not defined
 
  • #4
Line 58 and 60 you redefined print as a string.
 
  • Like
Likes sysprog, atyy and Greg Bernhardt
  • #5
jedishrfu said:
Line 58 and 60 you redefined print as a string.

Yes, I think that's it. I assume the intent was to have those lines print things, not set the variable "print" equal to things.
 
  • Like
Likes sysprog and jedishrfu
  • #6
Ah - I missed that. As per the previous two comments, you've stored a string in a variable called print. When you then try to call print the second time around the loop, python tries to call that string, which doesn't work.
 
  • Like
Likes sysprog and jedishrfu
  • #7
I haven’t tested your code on Pycharm but it might flag this kind of error so you can fix it sooner. I know it can flag code that violates the Pep rules.

UPDATE: I tested it on the PyCharm IDE and PyCharm underlined the print word and gave the warning that it shadows a built-in name "print". Additional commentary mentioned len and list as other commonly shadowed built-in names.

I did run it on Pythonista on iOS and it showed the print to be a keyword but didn’t warn me until I ran it And got your error.

With respect to variable naming, I always strive to use camel case Or underscores and to never use a single English word like print. Sometimes I'll keep it simple using short one letter variables for intermediate results like s1 or s2 for strings.
 
Last edited:
  • Like
Likes sysprog
  • #8
jedishrfu said:
i did run it on Pythonista on iOS and it showed the print to be a keyword

"print" isn't a keyword in Python 3. In Python 2 lines 58 and 60 in Greg's script would have been a syntax error.
 
  • Like
Likes sysprog and jedishrfu
  • #9
Curiously, even in Greg's listing the pretty printer plugin Geshi shows "print" highlighted as a keyword likely due to the fact that python2/3 is mapped to a generic python in the pretty printer plugin.
 
  • #10
jedishrfu said:
Line 58 and 60 you redefined print as a string.
Woohoo that was it! Always the silly things for me! Thanks!
 
  • Like
Likes sysprog, atyy and jedishrfu

1. What is a "str" object in Python?

A "str" object in Python is a data type that represents a string of characters. It can be used to store and manipulate text data in a program.

2. What does it mean when a "str" object is not callable?

When a "str" object is not callable, it means that you are trying to use it as a function but it is not designed to be called as such. This can happen if you try to pass arguments to a string or use parentheses after it, which are reserved for function calls.

3. How can I fix the "str" object is not callable error?

To fix this error, you need to make sure that you are using the "str" object correctly. Check that you are not trying to use it as a function and that you are using the correct syntax for the task you are trying to perform. You may also need to convert the "str" object to a different data type if necessary.

4. Can the "str" object is not callable error occur in other programming languages?

Yes, this error can occur in other programming languages. The specific error message may differ, but the underlying issue is the same - trying to use a string as a function when it is not designed to be called as such.

5. How can I prevent the "str" object is not callable error from happening?

To prevent this error, you should carefully read the documentation for the programming language you are using and make sure you understand how to use each data type correctly. You should also test your code regularly and handle any errors that may occur to prevent them from causing issues in your program.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
5K
Back
Top