如何跳过<p><h2><a……>而得到d

2024-09-30 22:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,我没有合适的词来称呼你。我要做的是这个代码通过给我所有的文本来满足我的要求。但问题是,在获取文本时,会出现诸如“<;p>;”、“<;a href….>;”、“<;h1>;”、“<;h2>;”之类的情况。。。。也在印刷。有人能帮我跳过那些标签吗? 我的代码:(我使用的是python2.7.8)

import urllib
from xml.etree.ElementTree import parse

# Download the RSS feed and parse it
u = urllib.urlopen('http://planet.python.org/rss20.xml')
doc = parse(u)

# Extract and output tags of interest
for item in doc.iterfind('channel/item'):
#    title = item.findtext('title')
#    date = item.findtext('pubDate')
#    link = item.findtext('link')
    des = item.findtext('description')
#    print(title)
#    print(date)
#   print(link)
    print(des)
    print()

Tags: and代码importltgtdatedoctitle
1条回答
网友
1楼 · 发布于 2024-09-30 22:22:46

尝试使用BeautifulSoup解析HTML内容 如果你只需要文本的话,这样的东西就行了。如果需要HTML内容中的特定信息,可以解析HTML。你知道吗

import urllib
from xml.etree.ElementTree import parse
from bs4 import BeautifulSoup as bs

# Download the RSS feed and parse it
u = urllib.urlopen('http://planet.python.org/rss20.xml')
doc = parse(u)

# Extract and output tags of interest
for item in doc.iterfind('channel/item'):
    des = item.findtext('description')
    if des:
        soup = bs(des)
        text = soup.get_text()
        print(text.encode('utf-8'))

相关问题 更多 >