[英]TypeError: 'str' object is not callable
我在程序中添加了几行代码,以使用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`
在运行代码。 谁能告诉我解决此错误的正确方法。
您正在尝试在此处“调用”字符串:
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")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.