【问题标题】:Regular expression to extract data from news page从新闻页面中提取数据的正则表达式
【发布时间】:2016-10-08 13:16:56
【问题描述】:

您好,我正在运行 python 正则表达式来从新闻页面中提取一些数据,但是当它显示时,代码会在输出中产生括号和撇号。例如这是我的代码:

description_title = findall('<item>[\s]*<title[^>]*>(.*?)<\/title>[\s]*<description>', html_source)[:1]
        news_file.write('<h3 align="Center">' + str(description_title) + ": " + '</h3\n>')

但是这段代码创建了 ['Technology']:, ['Finance']: 但我想要没有 [''] 的 Technology, Finance 的输出。

【问题讨论】:

标签: python html regex


【解决方案1】:

通过使用str,您正在打印description_title 的Python 字符串表示形式(即长度为1 的list)。尝试不使用str

'<h3 align="Center">' + description_title[0] + ": " + '</h3\n>'

【讨论】:

  • 如果事实上,str 没有定义,在这种特殊情况下它调用repr。它返回 Python list 的字符串表示形式。
猜你喜欢
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
相关资源
最近更新 更多