【发布时间】:2015-03-15 15:56:16
【问题描述】:
我是正则表达式的新手,所以我希望这不是一个太明显的问题
我正在寻找 craigslist 公寓列表的 html 中的社区。社区是这样列出的
(castro / upper market)
</h2>
这是一个html的例子......
<a class="backup" disabled="disabled">▲</a>
<a class="next" disabled="disabled"> next ▶</a>
</span>
</section>
<h2 class="postingtitle">
<span class="star"></span>
$5224 / 2br - Stunning Furnished 2BR with Hardwwod Floors & Newly renovated Kitchen (pacific heights)
</h2>
<section class="userbody">
<figure class="iw">
<div class="slidernav">
<button class="sliderback"><</button>
<span class="sliderinfo"></span>
<button class="sliderforward">></button>
这应该找到所有不同的社区
但是在整个 html 页面上花费的时间太长了
\w+\s?(\/)?\s?\w+\s?(\/)?\s?\w+\s?(\/)?\s?\w+\)\n<\/h2>
# \w+ to find the word
# \s?(\/)?\s? for a space or space, forward slash, space
# \n<\/h2> because </h2> is uniquely next to the neighborhood in the html
有没有办法找到
</h2>
那么在后面寻找附近的文本字符串?
非常感谢任何帮助或引导我朝着正确的方向前进
【问题讨论】:
-
对 html 使用正则表达式并不是一个好主意 (more here)。使用适当的工具,例如scrapy.org。
标签: python html regex web-scraping html-parsing