【问题标题】:My scraper fails to grab few contents我的刮刀无法抓取少量内容
【发布时间】:2018-12-16 09:10:23
【问题描述】:

我使用python 结合re 模块编写了一个脚本来解析网页中的一些内容。我希望解析的内容:其中一些有空格,而另一些则没有。我怎样才能将它们全部包含在一个模式中。我试过的只能抓住周围有空格的。我使用了积极的后视和积极的前瞻来获得它们。

我想使用正则表达式从下面的html elements 中获取文本asked

import re

content = """
<div class="user-action-time">
          asked <span title="2018-07-08 09:43:08Z" class="relativetime">2 hours ago</span>
    </div>

<div class="user-action-time">asked<span title="2018-07-07 17:17:07Z" class="relativetime">18 hours ago</span>
    </div>

<div class="user-action-time">
          asked <span title="2018-07-06 20:35:48Z" class="relativetime">yesterday</span>
    </div>
"""
pattern = re.compile(r'(?<=user-action-time">\s)(.*)(?=<span)')
for item in pattern.finditer(content):
    print(item.group())

【问题讨论】:

    标签: python regex python-3.x web-scraping


    【解决方案1】:

    在我的示例中,我提取了整个 div 的文本内容以及如何仅提取 div 内 span 的内容。我建议使用某种 HTML 解析器。我在示例中使用了 lxml html,但还有很多其他的。

    from lxml import html
    
    content = """
    <div class="user-action-time">
              asked <span title="2018-07-08 09:43:08Z" class="relativetime">2 hours ago</span>
        </div>
    
    <div class="user-action-time">asked<span title="2018-07-07 17:17:07Z" class="relativetime">18 hours ago</span>
        </div>
    
    <div class="user-action-time">
              asked <span title="2018-07-06 20:35:48Z" class="relativetime">yesterday</span>
        </div>
    """
    
    tree = html.fromstring(content)
    
    user_action_times = [e.text_content() for e in tree.iter('div') if e.get('class') == 'user-action-time']
    relative_time = [e.text_content() for e in tree.iter('span') if e.get('class') == 'relativetime' and e.get('title') is not None]
    
    print (user_action_times)
    print (relative_time)
    

    【讨论】:

    • 如果是关于使用BeautifulSouplxml 解析器获取它们,我可以自己处理它们,我会将它们用作问题tag。我正在尝试学习正则表达式。希望你能理解。顺便谢谢你的回答。
    • 我的错,我只是对你为什么要使用正则表达式来解析 HTML 感到困惑,但如果你想学习正则表达式,那就是另一回事了!那我应该删除这个答案吗?
    • 不,你不应该这样做,因为如果有人尝试使用解析器,他可能会从你的方法中获得帮助。
    【解决方案2】:

    由于您想从字符串中捕获一个单词,即“asked”,因此如果您一直希望获取一个单词,则以下方法应该可以工作

    INPUT(在底部的打印语句中)

    import re
    content = """
    <div class="user-action-time">
              asked <span title="2018-07-08 09:43:08Z" class="relativetime">2 hours 
    ago</span>
        </div>
    
    <div class="user-action-time">asked<span title="2018-07-07 17:17:07Z" 
    class="relativetime">18 hours ago</span>
        </div>
    
    <div class="user-action-time">
              asked <span title="2018-07-06 20:35:48Z" 
    class="relativetime">yesterday</span>
        </div>
    """
    
    
    print(re.findall('<div[\S\s]*?>[\s]*([\S]+?)[\s<]+', content))
    

    输出

    ['asked', 'asked', 'asked']
    

    .

    .

    现在,如果您需要捕获一个句子,则必须对正则表达式进行一些处理(我使用的那个很脏,但很有效——再次,可能会有一些细微的细微差别,具体取决于内容字符串,这可能影响其性能)

    INPUT SENTENCES(在底部的打印语句中)

    import re
    content = """
    <div class="user-action-time">
              asked my friend <span title="2018-07-08 09:43:08Z" 
    class="relativetime">2 hours 
    ago</span>
        </div>
    
    <div class="user-action-time">asked my dad<span title="2018-07-07 17:17:07Z" 
    class="relativetime">18 hours ago</span>
        </div>
    
    <div class="user-action-time">  asked my mom <span title="2018-07-07 17:17:07Z" 
    class="relativetime">18 hours ago</span>
        </div>
    
    <div class="user-action-time">
              asked <span title="2018-07-06 20:35:48Z" 
    class="relativetime">yesterday</span>
        </div>
    """
    
    
    print(re.findall('<div[\S\s]*?>[\s]*([\S\s]+?[\S])[\s]*[<]+', content))
    

    输出

    ['asked my friend', 'asked my dad', 'asked my mom', 'asked']
    

    【讨论】:

      【解决方案3】:

      好像是我自己做的。这是我试图让他们得到的:

      pattern = re.compile('(?<=user-action-time">)(\s*?.*)(?=<span)')
      for item in pattern.finditer(content):
          print(item.group().strip())
      

      输出:

      asked
      asked
      asked
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-04
        • 2018-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-09
        • 2019-10-09
        • 1970-01-01
        相关资源
        最近更新 更多