-1
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')

why it is showing error on line 14

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • What do you expect `ord(alpha) += 1` to **do**. That is indeed not valid Python, because you cannot assign to `ord(alpha)`. – Martijn Pieters Jan 10 '15 at 15:36
  • Possible duplicate of [Fix "can't assign to function call" error](http://stackoverflow.com/questions/5964927/fix-cant-assign-to-function-call-error) – Mark Amery Jan 01 '17 at 22:28

1 Answers1

2

You cannot assign to ord(alpha), no. If you wanted to get the next letter in the alphabet, you'll have to do a little more work:

alpha = chr(ord(alpha) + 1)

That creates a new character from the ordinal of the current character, plus one.

Far more pythonic would be to use the string.ascii_uppercase string in a loop:

import string

# ...

for alpha in string.ascii_uppercase:

This'll loop once over all uppercase letters in the ASCII standard; you can always use break if you need to limit the loop to maxalphabet at some point, or use a numeric limit (between 0 and 26) and use slicing. However you are not even using the alpha variable anywhere in your loop.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343