简体   繁体   中英

Python beautiful soup syntax error

I'm using an example I found that extracts links from an webpage

from BeautifulSoup import BeautifulSoup
import urllib2
import re

html_page = urllib2.urlopen("http://arstechnica.com")
soup = BeautifulSoup(html_page)
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
print link.get('href')

I get:

File "html_test.py", line 9
    print link.get('href')
             ^
SyntaxError: invalid syntax

I've installed BeautifulSoup with pip to no success.

Instead of

from BeautifulSoup import BeautifulSoup

use:

from bs4 import BeautifulSoup

Also, in Python3, print is a function, not a statement:

print(link.get('href'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM