首页 新闻 赞助 找找看

代码提示并未报错,运行却出错,咋回事?

0
悬赏园豆:10 [已关闭问题] 关闭于 2016-05-07 16:45
 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

KirkZheng的主页 KirkZheng | 初学一级 | 园豆:116
提问于:2016-05-06 20:28
< >
分享
所有回答(1)
0

你引用的bs4是Python 2,不可以在Python 3中运行,提示说的很清楚嘛!

刘宏玺 | 园豆:14020 (专家六级) | 2016-05-06 21:36

关键是3不知道怎么导入bs,帮忙写一下呗

支持(0) 反对(0) KirkZheng | 园豆:116 (初学一级) | 2016-05-06 22:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册