There was an error in scraping (TypeError: 'str' object is not callable), but I don't know what to do.

Asked 1 years ago, Updated 1 years ago, 112 views

Prerequisites/What you want to achieve

When I was trying to code to get abema's program title using selenium in python, an error such as TypeError: 'str' object is not callable occurred.

Problems/Error Messages you are experiencing

TypeError: 'str' object is not callable

Source Codes Affected

The following are the areas where the error occurred:

#Title
  title=item.find_elements_by_xpath("div[1]/div/p/span/div/span")
  iflen(title):
    print(str(cnt)+":"+title[0].text())

The entire code should look like this

 from typing import Container
import urllib.request
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from time import sleep, time

options=Options()
options.add_argument('--headless')


driver=webdriver.Chrome('C:/Users/Username/Downloads/chromedriver_win32/chromedriver', chrome_options=options)

driver.get('https://abema.tv/timetable')
sleep(10)
driver.find_element_by_xpath("//button [contains(text(), '19 years and under')]US>") .click()
driver.find_element_by_xpath("//button [contains(text(), 'male')]").click()
driver.find_element_by_xpath("//button [contains(text(), 'Next')]US>") .click()
sleep(10)
driver.find_element_by_class_name("com-genre-survey-GenreSurveyCard_text").click()
driver.find_element_by_xpath("//button [contains(text(), 'completed')]US>") .click()
sleep(10)
category=driver.find_element_by_xpath("//a[@class='com-timetable-ChannelIconHeader__channel-link com-a-Link com-a-Link --dim' ]")
img = category.find_elements_by_xpath("picture/img")
iflen(img):
  src=img[0].get_attribute('src')
  urllib.request.urlretrieve(src, "img.png")
category.click()
sleep(10)
timerable=driver.find_elements_by_xpath("//div [@class='com-timetable-timableItem__wrapper']")
cnt = 0
for item intimitable:
  cnt+=1
  # title
  title=item.find_elements_by_xpath("div[1]/div/p/span/div/span")[0].get_attribute("innerHTML").text()
  iflen(title):
    print(str(cnt)+":"+title)

Tried

Refer to the following site (https://stackoverflow.com/questions/55488606/typeerror-str-object-is-not-callable-using-selenium-through-python)

 title=item.find_elements_by_xpath("div[1]/div/p/span/div/span")[0].text
print(str(cnt)+":"+title)
 title=item.find_elements_by_xpath("div[1]/div/p/span/div/span")[0].get_attribute("innerHTML").text()
  print(str(cnt)+":"+title)

and so on, but the same error occurred

Supplementary information (for example, FW/Tool Version)

Python 3.9.5
selenium3.141.0
ChromeDriver 92.0.4515.107

python web-scraping

2022-09-30 16:40

2 Answers

It has been resolved as a result of text instead of text().
For more information, visit https://teratail.com/questions/352245#reply-482192


2022-09-30 16:40

First, remember that everything is an object in Python.Functions are also specialized in these objects, which are "callable" objects.For example, if you call a function in func(), go to the function definition and run the method __call()__ for that function definition.The error 'str' object is not callable means that you are trying to invoke the string method __call()__ without that method.


2022-09-30 16:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.