【问题标题】:Syntax error in for line in pythonpython中for行的语法错误
【发布时间】:2013-09-09 02:49:48
【问题描述】:

我正在运行一个 python 脚本。我在for 行中遇到无法解释的语法错误。 这是代码:

today = datetime.date.today()
url="http://www.99acres.com/property-in-velachery-chennai-south-ffid?"
print "INSERT INTO Property (URL,Rooms, Place, Phonenumber1,Phonenumber2,Phonenumber3,Typeofperson, Name)"
print "VALUES ("

page=urllib2.urlopen(url)
    soup = BeautifulSoup(page.read())
    properties = soup.findAll(('a', {'title':re.compile('Bedroom')}),('i',{'class':'pdate'})
    for eachproperty in properties:
     print today,","+ "http:/" + eachproperty['href'] ",", eachproperty.string"," ,.join(re.findall("'([a-zA-Z0-9,\s]*)'", eachproperty['onclick'])) 
     print ")"

错误是

$ python properties.py
  File "properties.py", line 15
    for eachproperty in properties:
                                  ^
SyntaxError: invalid syntax

更新

下面这行正确吗?

properties = soup.findAll(('a', {'title':re.compile('Bedroom')}),('i',{'class':'pdate'}))

【问题讨论】:

  • 统计上一行)(的个数。
  • @AshwiniChaudhary 我已经更新了这个问题,还有一个疑问。因为,没有显示错误。但也没有输出

标签: python beautifulsoup screen-scraping


【解决方案1】:

前面的行与右括号的数量相比,打开(括号的计数不正确:

properties = soup.findAll(('a', {'title':re.compile('Bedroom')}),('i',{'class':'pdate'})
#                      --^^                     ---^      ---^-^-^                -----^

再添加一个关闭)

properties = soup.findAll(('a', {'title':re.compile('Bedroom')}),('i',{'class':'pdate'}))

【讨论】:

    猜你喜欢
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多