user3932077
user3932077

Reputation: 77

TypeError: 'str' object is not callable

I have added few lines of code to my program to convert a html to json using BeautifulSoup but getting an error for those added lines of code.

import httplib, urllib
from bs4 import BeautifulSoup
import json

params = urllib.urlencode({'cmm': 'onion', 'mkt': '', 'search': ''})
headers = {'Cookie': 'ASPSESSIONIDCCRBQBBS=KKLPJPKCHLACHBKKJONGLPHE; ASP.NET_SessionId=kvxhkhqmjnauyz55ult4hx55; ASPSESSIONIDAASBRBAS=IEJPJLHDEKFKAMOENFOAPNIM','Origin': 'http://agmarknet.nic.in', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-GB,en-US;q=0.8,en;q=0.6','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control': 'max-age=0','Referer': 'http://agmarknet.nic.in/mark2_new.asp','Connection': 'keep-alive'}
conn = httplib.HTTPConnection("agmarknet.nic.in")
conn.request("POST", "/SearchCmmMkt.asp", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
htmldata = [[cell.text for cell in row("td")]for row in BeautifulSoup((data)("tr"),"lxml")]
x = json.dumps(dict(htmldata))
print x

I am getting an error as

Traceback (most recent call last):
  File "commodity.py", line 12, in <module>
    data1 = [[cell.text for cell in row("td")]for row in BeautifulSoup((data)("tr"),"lxml")]
TypeError: 'str' object is not callable`enter code here`

on running the code. Can anyone tell me the correct approach to resolve this error.

Upvotes: 1

Views: 2020

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121914

You are trying to 'call' a string here:

BeautifulSoup((data)("tr"),"lxml")

(data) is a string, and (data)("tr") is a call to the string.

Perhaps you wanted to find all <tr> elements:

BeautifulSoup(data, "lxml").find_all("tr")

making the full statement:

htmldata = [[cell.text for cell in row("td")] for row in BeautifulSoup(data, "lxml").find_all("tr")]

Upvotes: 1

Related Questions