0

首先,我想强调我不是程序员。


我已经编写了这段代码,但我无法运行它。我使用 Visual Studio 2013,我有以下问题:

  • print "Beginning %s" % tag --- 无效的语法

  • print sou*p*.find('div', {'id': 'bar'}).p.text -----无效语法

这是我写的代码。它旨在检索所选标签的内容。

from bs4 import BeautifulSoup
from HTMLParser import HTMLParser
import urllib

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print "Beginning %s" % tag
    def handle_endtag(self, tag): 
        print "End %s tag" % tag
    def handledata(self, data):
        print "Data %s" % data
p = MyHTMLParser()
input = urllib.urlopen('http://php.com')
html = input.read()
input.close()
soup = BeautifulSoup(p.feed(html))
print soup.find('div', {'id': 'bar'}).p.text
4

0 回答 0