Python正则表达式网页

时间:2014-02-02 09:47:03

标签: python html regex

我需要帮助为网页编写正则表达式来提取一些数据。该网页是: 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>")

1 个答案:

答案 0 :(得分:1)

嗯,解决问题的另一种方法是使用regex函数,而不是使用split

s.split('</a>')[0].split('>')[1].split(' ')[0]

应该返回你想要的答案。

然而,使用上述方法对于更复杂的HTML来说变得乏味。您可以改用HTMLParser模块。