在Python中使用str(count)时遇到问题
嘿,我正试图写一个脚本来搜集新产品信息的网站。我已经跑得很远了,但当我跑的时候它说在Python中使用str(count)时遇到问题,python,Python,嘿,我正试图写一个脚本来搜集新产品信息的网站。我已经跑得很远了,但当我跑的时候它说 print str(count) ^ SyntaxError: invalid syntax 我对Python非常陌生,这让我头疼。这是我的全部代码: import requests session=requests.session() headers={ ':host':'launches.endclothing.com', 'accept':'application/js
print str(count)
^
SyntaxError: invalid syntax
我对Python非常陌生,这让我头疼。这是我的全部代码:
import requests
session=requests.session()
headers={
':host':'launches.endclothing.com',
'accept':'application/json, text/plain, */*',
'accept-encoding':'gzip,deflate',
'content-type':'application/x-www-form-urlencoded; charset=UTF-8',
'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36'
}
for count in range(50):
try:
itemUrl='https://launches.endclothing.com/api/products/'+str(count)
itemRes=session.get(itemUrl,headers=headers)
print str(count)
print itemRes.json()['name']+' : '+itemRes.json()['colour']
print itemRes.json()['releaseDate']
print '\n'
except:
print 'N/A'
print '\n'
如果您使用的是Python3,那么您必须编写这样的代码
print (str(count))
我使用的是Python2.7,您的代码运行得很好。没有错误 正如@Frédéric Hamidi所述,如果您使用的是python 3.x
用print
(str(count))
替换print str(count)
,然后再试一次。我的水晶球显示您正在使用Python 3print()
是Python3中的一个函数。我怀疑Frederic是正确的。是的,非常感谢。只需补充一点:在Python2.7中,这种语法也会起作用。