我正在从我的网站上提取地址,因为我没有以xml格式进行备份。我得到了它的工作,除了现在我想用逗号划分城市和国家。
这是我到目前为止所拥有的
#!/usr/bin/env python2.7
from requests import get
from bs4 import BeautifulSoup as Soup
f = open('scraped.csv', 'wb')
f.write('"Name","URL","Address Line 1","new_line1","new_line2","Phone"\n')
rej = open('rejected.csv', 'wb')
rej.write('"ID"\n')
for i in xrange(1, 7397 + 1):
try:
url = "http://map.crossfit.com/affinfo.php?a={}&t=0".format(i)
text = get(url).text
splitted = [foo.replace('\n', ' ') for foo in text.split('<br />')]
soup = Soup(splitted[0])
_, addr1, new_line1 = line1.split(',')[0], new_line2 = line1.split(',')[1] + ', ' + line2, phone = [foo.replace('"', '""') for foo in splitted]
name = soup.text
url = soup.b.a['href']
line = '"' + '","'.join((name, url, addr1, addr2, phone)) + '"'
print line
f.write((line + '\n').encode('utf8'))
except KeyboardInterrupt:
break
except:
print 'Rejected: {}'.format(i)
rej.write('{}\n'.format(i))
f.close()
rej.close()
我得到的错误是:
File "/Users/Spencer/Downloads/xmlmaker.py", line 18
_, addr1, new_line1 = line1.split(',')[0], new_line2 = line1.split(',')[1] + ', ' + line2, phone = [foo.replace('"', '""') for foo in splitted]
SyntaxError: can't assign to operator
有什么想法吗?我看着,看到可能有些拼写错误,但我只是不知道。
答案 0 :(得分:4)
将这些陈述放在不同的行上:
_, addr1, new_line1 = line1.split(',')[0]
new_line2 = line1.split(',')[1] + ', ' + line2
phone = [foo.replace('"', '""') for foo in splitted]
使用;
分隔单行上的语句,
。但它的可读性较差,所以最好将它们放在不同的行上:
>>> x = 1; y = 2
>>> x,y
(1, 2)
来自PEP-8:
复合语句(同一行上的多个语句)是 一般都气馁。
答案 1 :(得分:3)
您不能将作业视为值,即=
左侧不能有表达式(每行只有一个=
,但a = b = c = 0
等链式作业除外)。替换怪物线
_, addr1, new_line1 = line1.split(',')[0], new_line2 = line1.split(',')[1] + ', ' + line2, phone = [foo.replace('"', '""') for foo in splitted]
类似
phone = [foo.replace('"', '""') for foo in splitted]
new_line2 = line1.split(',')[1] + ', ' + line2
_, addr1, new_line1 = line1.split(',')[0]