-1

我正在从我的网站中提取地址,因为我没有以 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

有任何想法吗?我正在寻找并看到可能有一些拼写错误,但我只是不知道。

4

2 回答 2

4

将这些语句放在单独的行中:

_, addr1, new_line1 = line1.split(',')[0]
new_line2 = line1.split(',')[1] + ', ' + line2
phone = [foo.replace('"', '""') for foo in splitted]

用于;在单行上分隔语句 not ,。但它的可读性较差,因此最好将它们放在单独的行中:

>>> x = 1; y = 2
>>> x,y
(1, 2)

来自PEP-8

通常不鼓励复合语句(同一行上的多个语句)。

于 2013-07-09T19:15:55.180 回答
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]
于 2013-07-09T19:16:58.437 回答