Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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 - Fatal编程技术网

Python正则表达式切片

Python正则表达式切片,python,html,regex,Python,Html,Regex,我正在尝试使用以下示例代码获取网页: from urllib import urlopen print urlopen("http://www.php.net/manual/en/function.gettext.php").read() 现在我可以在一个变量中获取整个网页。我想在页面上找到这样的内容 <div class="methodsynopsis dc-description"> <span class="type">string</span>

我正在尝试使用以下示例代码获取网页:

from urllib import urlopen
print urlopen("http://www.php.net/manual/en/function.gettext.php").read()
现在我可以在一个变量中获取整个网页。我想在页面上找到这样的内容

<div class="methodsynopsis dc-description">
   <span class="type">string</span><span class="methodname"><b>gettext</b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$message</tt></span>
   )</div>

stringgettext(字符串$message)
)
这样我就可以生成一个文件来在另一个应用程序中实现。
我希望能够提取单词“string”、“gettext”和“$message”。

从HTML提取信息时,不建议只将一些正则表达式组合在一起。正确的方法是使用适当的HTML解析模块。Python有几个很好的模块用于此目的,我特别推荐


不要因为这个名字而感到不快——这是一个严肃的模块,很多人都在使用它,并取得了巨大的成功。有很多例子可以帮助您开始了解您的特殊需求。

为什么不尝试使用BeautifulSoup

示例代码:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(htmldoc)
allSpans = soup.findAll('span', class="type")
for element in allSpans:
    ....

关于这个问题的各种说法已经被问过很多次了。这是明确的答案: