【问题标题】:I am unable to see output on pycharm console , code is without error我无法在 pycharm 控制台上看到输出,代码没有错误
【发布时间】:2015-08-11 20:05:13
【问题描述】:

我想打印特定网页上的所有网址。下面的代码没有错误,但无法在 pycharm 控制台上看到所需的结果。任何帮助将不胜感激 。在控制台上只出现了“你好”。提前非常感谢。

from sgmllib import SGMLParser
import urllib


class URLLister(SGMLParser):
    def reset(self):
        SGMLParser.reset(self)
        self.urls = []

    def start_a(self, attrs):
        href = [v for k, v in attrs if k == 'href']
        print href
        if href:
        self.urls.extend(href)


usock = urllib.urlopen("http://diveintopython.org/")
parser = URLLister()
parser.feed(usock.read())
print "hello"
usock.close()
parser.close()
for url in parser.urls:
   print url

【问题讨论】:

  • 在 'parser.close()' 之后设置断点并仔细查看 parser.urls。好像是空的。

标签: python html url pycharm urllib


【解决方案1】:
usock = urllib.urlopen("http://diveintopython.org/")

我怀疑你的意思是......

usock = urllib.urlopen("http://diveintopython.NET/")

...确实适用于您的代码。

【讨论】:

    猜你喜欢
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    相关资源
    最近更新 更多