新手一枚,想批量获取一批数据在后台自动生成的 ID 然后做匹配
本来是 for 循环,报错后拆开来还是报错那应该不是我循环的问题了
第一段能正常获取到 ID ,为什么第二段始终返回 NONE
Traceback (most recent call last):
File "
add_store.py", line 119, in <module>
main()
File "
add_store.py", line 113, in main
print result[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
代码如下:
(删掉了括号里的敏感信息)
hosturl =
posturl =
posturl2 =
cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
h = urllib2.urlopen(hosturl)
headers = {}
postData = {}
postData = urllib.urlencode(postData)
request = urllib2.Request(posturl, postData, headers)
response = urllib2.urlopen(request)
conn = MySQLdb.connect()
def main():
i = 31
stname = 'teststore' + str(i)
postData2 = {'storeName': stname}
postData2 = urllib.urlencode(postData2)
request2 = urllib2.Request(posturl2, postData2, headers)
response2 = urllib2.urlopen(request2)
cur = conn.cursor()
cur.execute("select a.store_id from store as a where a.store_name = %s", [stname])
print stname
result = cur.fetchone()
print result[0]
i = 32
stname = 'teststore' + str(i)
postData2 = {'storeName': stname}
postData2 = urllib.urlencode(postData2)
request2 = urllib2.Request(posturl2, postData2, headers)
response2 = urllib2.urlopen(request2)
cur.execute("select a.store_id from store as a where a.store_name = %s", [stname])
print stname
result = cur.fetchone()
print result[0]
cur.close()
conn.close()
if __name__ == '__main__':
main()
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/292502
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.