SyntaxError:无法分配给函数调用

时间:2015-01-10 15:34:17

标签: python python-3.x

import requests
from bs4 import BeautifulSoup

def spider(maxalphabet):
    alpha = 'A'
    while ord(alpha) <= ord(maxalphabet):
        url = 'http://djpunjab.us/m/page/punjabimusic/'
        source_code = requests.get(url)
        plaintext = source_code.text
        soup = BeautifulSoup(plaintext)
        for link in soup.findAll('a'):
            href = link.get('href')
            print(href)

        ord(alpha) += 1          # line no.14


spider('A')

为什么它在第14行显示错误

1 个答案:

答案 0 :(得分:2)

您无法分配到ord(alpha),不能。如果你想获得字母表中的下一个字母,你将需要做更多的工作:

alpha = chr(ord(alpha) + 1)

从当前角色的序号中创建一个新角色,加上一个。

更多pythonic将在循环中使用string.ascii_uppercase string

import string

# ...

for alpha in string.ascii_uppercase:

这个'循环遍及ASCII标准中的所有大写字母;如果您需要在某个时刻将循环限制为break,或者使用数字限制(介于0和26之间)并使用切片,则可以始终使用maxalphabet。但是,您甚至不能在循环中的任何位置使用 alpha变量。

相关问题