【问题标题】:Extracting particular string from a text file in Python从 Python 中的文本文件中提取特定字符串
【发布时间】:2020-04-26 23:34:15
【问题描述】:

您好,我在 TEXT 文件中有 HTML 代码的副本,因此我需要从该代码中提取少量信息,我设法这样做了,但我没有得到任何特定的模式来提取文本。

Position : 27
        <a href="https://www.fliegende-pillen.de/product/doppelherz-folsaeure-800-b-vitamine-tabletten.230477.html?p=466453&amp;noCS=1&amp;adword=google/PLA&amp;pk_campaign=google/PLA" id="vplap26" onmousedown="return google.arwt(this)" ontouchstart="return google.arwt(this)" class="plantl pla-unit-single-clickable-target clickable-card" rel="noopener noreferrer" target="_blank" aria-label="0,12 € / 1,00 St. Doppelherz Folsäure 800+B-Vitamine Tabletten 40 St. for €4.64 from fliegende-pillen.de" data-nt-icon-id="planti26" data-title-id="vplaurlt26" jsaction="mouseover:pla.sntiut;mouseout:pla.hntiut"></a>

Position : 28
        <a href="https://www.vitaminexpress.org/de/ultra-b-complex-vitamin-b-kapseln" id="vplap27" onmousedown="return google.arwt(this)" ontouchstart="return google.arwt(this)" class="plantl pla-unit-single-clickable-target clickable-card" rel="noopener noreferrer" target="_blank" aria-label="Ultra B Complex for €21.90 from vitaminexpress.org" data-nt-icon-id="planti27" data-title-id="vplaurlt27" jsaction="mouseover:pla.sntiut;mouseout:pla.hntiut"></a>

Position : 29
        <a href="https://www.narayana-verlag.de/Vitalstoff-Komplex-von-Robert-Franz-90-Kapseln/b22970" id="vplap28" onmousedown="return google.arwt(this)" ontouchstart="return google.arwt(this)" class="plantl pla-unit-single-clickable-target clickable-card" rel="noopener noreferrer" target="_blank" aria-label="Vitalstoff-Komplex - von Robert Franz - 90 Kapseln for €26.00 from Narayana Verlag" data-nt-icon-id="planti28" data-title-id="vplaurlt28" jsaction="mouseover:pla.sntiut;mouseout:pla.hntiut"></a>

我想提取“href”之后的 URL 链接,以及文本“aria-label”之后的产品名称。我如何在 Python 中做到这一点?

目前我正在使用下面的脚本来查找我感兴趣的行,

import psycopg2

try:

    filepath = filePath='''/Users/lins/Downloads/pladiv.txt''' 

    with open(filePath, 'r') as file:

       print('entered loop')
       cnt=1
       for line in file: 
        if 'pla-unit-single-clickable-target clickable-card" rel="noopener noreferrer" target="_blank" aria-label="' in line:
          print('Position : ' + str(cnt))
          cnt=cnt+1
          if 'href="' in line:
            print(line)
            fields=line.split(";")
            #print(fields[0] + '  as URL')

except (Exception, psycopg2.Error) as error:
        quit()

注意:我将其插入到我的 PostgreSQL 数据库中,上面示例中的代码已删除。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    你可以像这样使用正则表达式

    import re
    
    url = '<p>Hello World</p><a href="http://example.com">More Examples</a><a href="http://example2.com">Even More Examples</a>'
    
    urls = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', url)
    
    >>> print urls
    ['http://example.com', 'http://example2.com']
    

    或者您可以将文件解析为 HTML

    >>> from bs4 import BeautifulSoup as Soup
    >>> html = Soup(s, 'html.parser')           # Soup(s, 'lxml') if lxml is installed
    >>> [a['href'] for a in html.find_all('a')]
    ['http://example.com', 'http://example2.com']
    

    无论哪种方式都很好。 编辑 - 要获得 href 的全部价值,您可以使用它,

    url = """<a href="http://www.fliegende-pillen.de/product/doppelherz-folsaeure-800-b-vitamine-tabletten.230477.html?p=466453&amp;noCS=1&amp;adword=google/PLA&amp;pk_campaign=google/PLA" id="vplap26" onmousedown="return google.arwt(this)" ontouchstart="return google.arwt(this)" class="plantl pla-unit-single-clickable-target clickable-card" rel="noopener noreferrer" target="_blank" aria-label="0,12 € / 1,00 St. Doppelherz Folsäure 800+B-Vitamine Tabletten 40 St. for €4.64 from fliegende-pillen.de" data-nt-icon-id="planti26" data-title-id="vplaurlt26" jsaction="mouseover:pla.sntiut;mouseout:pla.hntiut"></a>"""
    
    findall = re.findall("(https?://[^\s]+)", url)
    
    print(findall)
    
    ['http://www.fliegende-pillen.de/product/doppelherz-folsaeure-800-b-vitamine-tabletten.230477.html?p=466453&amp;noCS=1&amp;adword=google/PLA&amp;pk_campaign=google/PLA"']
    

    【讨论】:

    • 非常感谢您的帮助,无论是 HTML 还是 TEXT 文件都可以,但主要要求是从 HTML 文件中的各个 DIVISIONS 中提取信息,不仅要找到 URL,还要找到 PRODUCT 名称。由于我是 Python 新手,没有找到比 TEXT 文件更好的方法并设法获取特定的行集,但想从这些行中获取 SUBSTRING 并提取名称和 URL。
    • 我也想要产品名称和它对应的 URL,所以这就是为什么。任何关于 i 的建议都会非常有帮助。
    • 从这一行中,我如何提取产品的 URL 和名称(名称通常出现在 TEXT aria-label=") vitaminexpress.org/de/ultra-b-complex-vitamin-b-kapseln" id="vplap27 " onmousedown="return google.arwt" ontouchstart="return google.arwt(this)" class="plantl pla-unit-single-clickable-target clickable-card" rel="noopener noreferrer" target="_blank" 咏叹调- label="来自vitaminexpress.org 的21.90 欧元的超B 复合物" data-nt-icon-id="planti27" data-title-id="vplaurlt27" jsaction="mouseover:">
    • 嘿@Linu 我已经编辑了我的答案,使用最后的版本
    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    相关资源
    最近更新 更多