【问题标题】:BeautifulSoup: Printing out also the start-tagBeautifulSoup:也打印出开始标签
【发布时间】:2013-09-09 11:20:06
【问题描述】:

实际上,我正在使用 BeautifulSoup。这段代码打印出 main 类的内容:

for text in soup.find_all("table", {'class', 'main'}):
        txt += text

这已经是一件好事了,但是怎么可能还包括“开始标签”,也就是这里的<class="main" ...>

非常感谢您的帮助! :)

【问题讨论】:

  • 不,开始标签是<table class="main">find_all() 不返回文本,它返回 bs4.Tag() 对象。你只是不匹配你认为你匹配的东西。 :-)

标签: python beautifulsoup


【解决方案1】:

你有一个 set 而不是 dictionary。做:

for text in soup.find_all("table", {'class':'main'}):
#                                          ^ colon here instead of a comma
        txt += text

【讨论】:

  • 但这并没有改变任何东西......第一个标签(类似于“")没有表示:(
【解决方案2】:
for el in soup.findAll('table', {'class':'main'}):
    print el.text         # text is here
    print el.attrs        # all attributes is here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2020-07-11
    • 1970-01-01
    • 2018-11-08
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多