我正在从我的网站中提取地址,因为我没有以 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
有任何想法吗?我正在寻找并看到可能有一些拼写错误,但我只是不知道。