Python美妙的汤语法错误

时间:2018-01-19 01:17:55

标签: python beautifulsoup

我正在使用我发现的一个示例,它从网页中提取链接

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

我明白了:

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

我已经使用pip安装了BeautifulSoup但没有成功。

1 个答案:

答案 0 :(得分:0)

而不是

from BeautifulSoup import BeautifulSoup

使用:

from bs4 import BeautifulSoup

另外,在Python3中,print是一个函数,而不是一个语句:

print(link.get('href'))
相关问题