Jasi
Jasi

Reputation: 463

Python Syntax Error: invalid syntax - if-codesyntax appears to be correct

I'm using Requests to scrape a website. The content of the html gets successfully saved in the variable r but in the if-statement I get the said error

[...]
for line in r:  
    link = re.findall(r ("""onclick="window.location.href='([^'])'""",line)
    if link: 
        print ('something')
        cmd = ('some commands to get info page') 
        call(cmd,shell=True)

        download = re.sub(something)
        cmd = ('some commands to download the file') 
        call(cmd,shell=True)
r.close()

I looked it up in the documentation and the syntax appears to be correct. I then suspected the error to be in the line before. Here I search for the line with the phrase onclick="window.location.href=' and want the link that follows it to be processed (in the code afterwards). The () encapsuled part should be what is returned, right?

Does anybody see an error? in

Upvotes: 0

Views: 1285

Answers (3)

user1444165
user1444165

Reputation:

Perhaps the brackets?

#                1  2                                                 2                   
link = re.findall(r ("""onclick="window.location.href='([^'])'""",line)

It looks like you forgot to close the bracket for findall.

Upvotes: 2

Andy
Andy

Reputation: 50610

You appear to have both mismatched parenthesis and mismatched quotation marks. Below, I've lined them up. Does this work as expected?

#                1  2                                  3    3           21
#                    123        4                              4321
link = re.findall(r ("""onclick="window.location.href='([^'])'\"""",line))

Upvotes: 0

R Hyde
R Hyde

Reputation: 10409

If you separate the pattern to its own line then it makes it clear that the problem is really just one of quoting. Try separating it like this:

for line in r:
    pattern = r"onclick=\"window.location.href='([^'])'"
    link = re.findall(pattern, line)

Upvotes: 0

Related Questions