TypeError:'str'对象不可调用

时间:2015-09-26 09:42:33

标签: python beautifulsoup bs4

我在我的程序中添加了几行代码,使用BeautifulSoup将html转换为json,但是为这些添加的代码行添加了错误。

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

我收到错误

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`

运行代码。任何人都可以告诉我解决此错误的正确方法。

1 个答案:

答案 0 :(得分:1)

您正试图在此处'调用'字符串:

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

(data)是一个字符串,(data)("tr")是对字符串的调用。

也许您想要找到所有<tr>元素:

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

制作完整的声明:

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