我在我的代码中使用BeautifulSoup来查找所有出现的' tr'和''但是在代码中第二次使用时收到错误。
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()
soup = BeautifulSoup(data,'lxml')
result = soup.findAll("tr")
for y in result:
row = BeautifulSoup(y,'lxml')
k = row.findAll("td")
for x in k:
text = x.text
print text
我收到以下错误
Traceback (most recent call last):
File "commodity.py", line 15, in <module>
row = BeautifulSoup(y,'lxml')
File "/usr/local/lib/python2.7/dist-packages/bs4/__init__.py", line 175, in __init__
markup = markup.read()
TypeError: 'NoneType' object is not callable.
任何人都可以帮我解决这个问题并告诉我使用它的正确方法。
答案 0 :(得分:1)
首先,您不应将Element
个对象传递给BeautifulSoup()
。只需完全删除BeautifulSoup(y, 'lxml')
来电:
for row in result:
cells = row.findAll("td")
for cell in cells:
text = cell.text
print text