代码在空闲中工作,但在Pycharm中不工作,这是完整的代码:
import sys
import urllib
import urllib2
from bs4 import BeautifulSoup
type_keyword = "唯品会与消协共同搭建绿色维权通道"
url = "http://www.baidu.com/s?wd=" + urllib.quote(type_keyword.decode(sys.stdin.encoding).encode('gbk'))
openpage = urllib2.urlopen(url).read()
soup = BeautifulSoup(openpage)
for child in soup.findAll("h3", {"class": "t"}):
geturls = child.a.get('href')
print urllib2.urlopen(geturls).geturl()单击"run“之后,Pycharm将按以下方式返回错误:
C:\Python27\python.exe C:/Python27/ParseWeb/ParseWeb.py
File "C:/Python27/ParseWeb/ParseWeb.py", line 3
SyntaxError: Non-ASCII character '\xe4' in file C:/Python27/ParseWeb/ParseWeb.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Process finished with exit code 1有趣的是,无论我在第3行中放了什么,Pycharm都会给出错误结果。
可能是什么原因?谢谢!
发布于 2017-03-14 05:10:49
您粘贴的代码不具有第3行中错误所指示的任何字符。
如果C:/Python27/ParseWeb/ParseWeb.py不是要运行的文件的名称,则可以尝试在PyCharm中打开要运行的文件,然后按Ctrl+Shift+F10 (运行当前打开的文件)。
还应该通过在文件的前两行中添加以下内容来定义Python文件的编码:
# -*- coding: utf-8 -*-这似乎是必要的,因为代码中有非ASCII字符,但首先要确保运行的是正确的文件。
发布于 2017-03-14 05:10:00
https://stackoverflow.com/questions/42777888
复制相似问题