1 import urllib.request 2 import bs4 3 import re 4 5 def getgxnu(): 6 url="http://www.gxnu.edu.cn/default.html" 7 data=urllib.request.urlopen(url).read() 8 page_data=data.decode('GBK') 9 '''''print(page_data)''' 10 soup = bs4(page_data) 11 #for link in soup.findAll('a',target='_self'):#get all links of gxnu index 12 # print(link) 13 for link in soup.findAll('a',href=re.compile('http://\\S+/type/\\d+.html')):#使用正则表达式 14 print(link['href'],link.contents) 15 16 17 #函数调用 18 getgxnu()
(使用的是python3x)
Traceback (most recent call last):
File "D:\eclipse\workspace\test\hello\HelloWorld.py", line 2, in <module>
import bs4
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2222, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2164, in _find_spec
File "<frozen importlib._bootstrap>", line 1940, in find_spec
File "<frozen importlib._bootstrap>", line 1916, in _get_spec
File "<frozen importlib._bootstrap>", line 1897, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 863, in spec_from_loader
File "<frozen importlib._bootstrap>", line 904, in spec_from_file_location
File "D:\Python34\lib\site-packages\beautifulsoup4-4.4.1-py3.4.egg\bs4\__init__.py", line 48
'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'
^
SyntaxError: invalid syntax
你引用的bs4是Python 2,不可以在Python 3中运行,提示说的很清楚嘛!
关键是3不知道怎么导入bs,帮忙写一下呗