使用正则表达式提取python

时间:2015-05-30 21:30:29

标签: python regex

我已经解析了包含带有beautifulsoup的javascript的html文档,并设法隔离其中的javascript并将其转换为字符串。 javascript看起来像这样:

<script>
    [irrelevant javascript code here]
    sources:[{file:"http://url.com/folder1/v.html",label:"label1"},
    {file:"http://url.com/folder2/v.html",label:"label2"},
    {file:"http://url.com/folder3/v.html",label:"label3"}],
    [irrelevant javascript code here]
</script>

我正在尝试获取一个只有这个sources数组中包含url的数组,看起来像这样:

urls = ['http://url.com/folder1/v.html', 
        'http://url.com/folder2/v.html', 
        'http://url.com/folder3/v.html']

域名是未知的IP,文件夹是随机名称长度,由小写字母和数字组成,每个文件中有1-5个(通常为3个)。所有不变的是,它们以http开头,以.html结束。

我决定使用正则表达式来处理这个问题(我很陌生),我的代码如下:urls=re.findall(r'http://[^t][^s"]+', document)

[^t]存在,因为文档中还有其他网址,其域名以t开头。我的问题是,有另一个网址与我提取的网址在同一个域中的jpg,它与其他网址一起被放入urls数组。

示例:

urls = ['http://123.45.67.89/asodibfo3ribawoifbadsoifasdf3/v.html'
        'http://123.45.67.89/alwefaoewifiasdof224a/v.html',
        'http://123.45.67.89/baoisdbfai235oubodsfb45/v.html',
        'http://123.45.67.89/i/0123/12345/aoief243oinsdf.jpg']

我如何才能获取html网址?

3 个答案:

答案 0 :(得分:3)

您可以使用 ls * 1: prop 2: prop 3: $ find . -mindepth 1 -maxdepth 1 -type d '!' -exec test -e "{}/prop" ';' -print ./3 获取文字中的网址:

r'"(http.*?)"'

用于提取您可以使用的>>> s="""<script> ... [irrelevant javascript code here] ... sources:[{file:"http://url.com/folder1/v.html",label:"label1"}, ... {file:"http://url.com/folder2/v.html",label:"label2"}, ... {file:"http://url.com/folder3/v.html",label:"label3"}], ... [irrelevant javascript code here] ... </script>""" >>> re.findall(r'"(http.*?)"',s,re.MULTILINE|re.DOTALL) ['http://url.com/folder1/v.html', 'http://url.com/folder2/v.html', 'http://url.com/folder3/v.html'] 网址列表中的.html

str.endswith

另外,作为此类任务的另一种通用且灵活的方法,您可以使用look for directories that don't contain your specific file模块:

>>> urls = ['http://123.45.67.89/asodibfo3ribawoifbadsoifasdf3/v.html',
...         'http://123.45.67.89/alwefaoewifiasdof224a/v.html',
...         'http://123.45.67.89/baoisdbfai235oubodsfb45/v.html',
...         'http://123.45.67.89/i/0123/12345/aoief243oinsdf.jpg']
>>> 
>>> [i for i in urls if i.endswith('html')]
['http://123.45.67.89/asodibfo3ribawoifbadsoifasdf3/v.html', 
 'http://123.45.67.89/alwefaoewifiasdof224a/v.html', 
 'http://123.45.67.89/baoisdbfai235oubodsfb45/v.html']

答案 1 :(得分:1)

如果格式始终与{file:url相同,请查找{file:后引号之间的子字符串:

s="""<script>
    [irrelevant javascript code here]
    sources:[{file:"http://url.com/folder1/v.html",label:"label1"},
    {file:"http://url.com/folder2/v.html",label:"label2"},
    {file:"http://url.com/folder3/v.html",label:"label3"}],
    [irrelevant javascript code here]
</script>"""


print(re.findall("\{file\:\"(.*?)\"",s))
['http://url.com/folder1/v.html', 'http://url.com/folder2/v.html', 'http://url.com/folder3/v.html']

您还可以通过在源代码上拆分一次来限制字符串搜索:

s="""<script>
    [irrelevant javascript code here]
    sources:[{file:"http://url.com/folder1/v.html",label:"label1"},
    {file:"http://url.com/folder2/v.html",label:"label2"},
    {file:"http://url.com/folder3/v.html",label:"label3"}],
    [irrelevant javascript code here]
</script>"""

print(re.findall("\{file\:\"(.*?)\"",s.split("sources:[",1)[1]))

哪会删除sources:[之前的所有其他行,假设没有其他sources:[

答案 2 :(得分:1)

这样的东西?

re.findall(r'http://[^t][^s"]+\.html', document)
相关问题