2015-09-26 89 views
0

我已經添加了幾行代碼到我的程序來使用BeautifulSoup將html轉換爲json,但爲這些添加的代碼行獲取錯誤。TypeError:'str'對象不可調用

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

你是怎麼想到'BeautifulSoup((數據)( 「TR」), 「LXML」 )'做什麼? '(data)(「tr」)'試圖調用字符串'data'。 –

+0

https://docs.python.org/2/library/httplib.html#httplib.HTTPResponse.read – Alik

+0

錯誤中顯示的變量「data1」後來被替換爲「htmldata」。所以這就是y中存在不匹配錯誤變量名稱和代碼變量名稱,但它在這裏沒有任何意義,因爲它稍後僅用於測試。 – user3932077

回答

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")]