Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式:找到一个字符串,然后向后看_Python_Html_Regex_Web Scraping_Html Parsing - Fatal编程技术网

Python 正则表达式:找到一个字符串,然后向后看

Python 正则表达式:找到一个字符串,然后向后看,python,html,regex,web-scraping,html-parsing,Python,Html,Regex,Web Scraping,Html Parsing,我对regex不熟悉,所以我希望这不是一个太明显的问题 我在craigslist公寓列表的html中查找邻居。这个社区是这样列出的 (castro / upper market) </h2> (卡斯特罗/高端市场) 这里是一个html的例子 <a class="backup" disabled="disabled">&#9650;</a> <a class="next" disabled="disabled"> next &#9

我对regex不熟悉,所以我希望这不是一个太明显的问题

我在craigslist公寓列表的html中查找邻居。这个社区是这样列出的

(castro / upper market)
</h2>
(卡斯特罗/高端市场)
这里是一个html的例子

<a class="backup" disabled="disabled">&#9650;</a>
<a class="next" disabled="disabled"> next &#9654;</a>
</span>

</section>

<h2 class="postingtitle">
<span class="star"></span>
&#x0024;5224 / 2br - Stunning Furnished 2BR with Hardwwod Floors &amp; Newly  renovated Kitchen (pacific heights)
</h2>
<section class="userbody">
<figure class="iw">


<div class="slidernav">
    <button class="sliderback">&lt;</button>
    <span class="sliderinfo"></span>
    <button class="sliderforward">&gt;</button>
和#9650;
下一个▶;
$;5224/2br-带硬木地板和;新装修厨房(太平洋高地)
这应该可以找到所有不同的社区

但是在一整页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
\w+\s?(\/)?\s?\w+\s?(\/)?\s?\w+\s?(\/)?\s?\w+\)\n
#\w+查找单词
#\s?(\/)?\s?对于空格或空格,正斜杠,空格
#\n因为在html中唯一地位于邻居旁边
有办法找到吗

</h2>

然后在后面查找邻近的文本字符串


非常感谢您的帮助或指导我朝着正确的方向前进

使用HTML解析器提取标题(
h2
标记内容),然后使用正则表达式提取邻域(括号内的文本)

示例(使用):

打印太平洋高地

请注意
\(.*?)$
正则表达式-它将包含字符串结尾前括号内的所有内容


使用,您可以在一行中求解它,因为
Selector
s具有。“刮壳”中的示例:


另请参见不应将正则表达式用于HTML解析的100个原因:


假设您的HTML存储在名为
page
的变量中,该模式如何

re.findall("\(([^\(\)]+)\)\n<\/h2>", page)

如果每页只有一个位置,
re.search()。请记住,
search()
生成一个中间匹配对象,而不是字符串本身。

使用string.find查找正则表达式索引,然后返回该索引处的负值

 In [1]: import re

 In [2]: c = "123456</h2>7890"

 In [3]: x = c.find("</h2>")

 In [4]: print c[x-6:x]
 123456
[1]中的
:导入re
在[2]中:c=“1234567890”
在[3]中:x=c.find(“”)
在[4]中:打印c[x-6:x]
123456

对html使用正则表达式不是一个好主意()。使用合适的工具,例如。谢谢,但这样会在一些主题上抓取太多的文本,这意味着在你想要避免的地方有一个较早的左括号。我正在修正模式以适应这种情况。你可能是对的,目前需要6秒钟来浏览大约5000个列表,到目前为止拿出了大约20个功能。当我有时间重做所有的工作时,我会仔细看看这个it@DavidFeldman当然,开始研究scrapy,并通过一个包含爬行器、项目和管道的scrapy项目来组织和模块化您的代码。实际上,解析HTML和从(web)页面提取内容并不完全是一回事。虽然您不应该用正则表达式解析HTML,但对于这种特殊情况,使用精心编制的RE进行提取可能要快一个数量级以上,我敢打赌。@fnl如果我们讨论的是从单个页面提取文本的速度,那么这就是问题所在。可读性、复杂性、可靠性等如何?有一些特定的格式和专门的工具专门用来解析这些格式,经过大量用户的测试和使用,证明是有效的。@alecxe-Hehe,当然可以。我很欣赏你对这件事突然采取的胡塞尔式的态度:)
re.findall("\(([^\(\)]+)\)\n<\/h2>", page)
re.findall("\(([^\(\)]+)\)\s*\n\s*<\/h2>", page)
neighborhoods = re.compile( "\(([^\(\)]+)\)\s*\n\s*<\/h2>")

# somewhere else, for each page 
for nh in neighborhoods.findall(page):
    print(nh)
pacific heights
 In [1]: import re

 In [2]: c = "123456</h2>7890"

 In [3]: x = c.find("</h2>")

 In [4]: print c[x-6:x]
 123456