无效的语法字符串比较python wiki解析器

时间:2014-02-25 16:47:11

标签: python

现在在从3.3.4恢复后使用python 2.7

我正在使用

import sys
import urllib
import urllib2
from bs4 import BeautifulSoup

article = sys.argv[1]
articleURL = urllib.quote(article)
print article
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

MAX_HOPS = 100
count=1

While article!="Philosophy" and count<MAX_HOPS:
    resource = opener.open("http://en.wikipedia.org/wiki/" + articleURL)
    data = resource.read()
    resource.close()
    soup = BeautifulSoup(data)
    print soup.find('div',id="bodyContent").p
    count+=1

for while article!=“Philosophy”我收到指向文章的无效语法错误。任何想法我做错了

2 个答案:

答案 0 :(得分:1)

如果您的代码段是您正在执行的确切代码,则会产生以下异常:

File "<stdin>", line 1
    While article!="Philosophy" and count<MAX_HOPS:
                ^
SyntaxError: invalid syntax

问题是行W中的大写While article!="Philosophy" and count<MAX_HOPS:,即使回溯指向文章。

解决方案很简单。使用while代替While

关于Python SyntaxError,有时回溯并不完美。如果输出有点混乱,请在指示的行/部分之前查看一下。

答案 1 :(得分:0)

它是while,而不是While(即小写w)。解释器认为While是一个名称,它试图解析一个赋值或一个表达式,它在article处失败。

相关问题