【发布时间】:2014-02-02 09:47:02
【问题描述】:
我需要帮助为网页编写正则表达式以提取一些数据。网页是: http://www.city-data.com/city/Addison-Texas.html
我想从这段 html 代码中返回“达拉斯”:
<a href="/county/Dallas_County-TX.html">Dallas County</a>
</p>
<b>Population in 2012:</b>
这是我到目前为止写的正则表达式,但它似乎不起作用。知道我做错了什么吗?
(">(.)/sCounty</a>\n</p>\n<b>Population in 2012:</b>")
【问题讨论】:
-
空格不是
/s,而是\s。 -
我仍然收到同样的错误:Traceback (last last call last): File "
", line 1, in IndexError: list index out of range -
采取this question中的解决方案之一。您不想在 HTML 上使用正则表达式,因为正则表达式无法解析 HTML。
-
秘诀是永远不要使用正则表达式来解析 html。 stackoverflow.com/questions/1732348/…
-
(.)匹配单个字符。