Как найти определенный текст в файле формата HTML - PullRequest
2 голосов
/ 11 июня 2019

Итак, я работаю с этим URL (http://www.ancient -hebrew.org / m / dictionary / 1000.html ).

Ниже мой код.

from bs4 import BeautifulSoup
import re

raw_html = open('/Users/gansaikhanshur/TESTING/webScraping/1000.html').read()
# lxml is faster. If you don't have it, pip install lxml
html = BeautifulSoup(raw_html, 'lxml')

# outputs: "http://www.ancient-hebrew.org/files/heb-anc-sm-beyt.jpg"
images = html.find_all('img', src=re.compile('.jpg$'))
for image in images:
    image = re.sub(
        r"..\/..\/", r"http://www.ancient-hebrew.org/", image['src'])
    # print(image)

# outputs: "unicode_hebrew_text"
fonts = html.find_all('font', face="arial", size="+1")
for f in fonts:
    f = f.string.strip()
    print(f)

# outputs: "http://www.ancient-hebrew.org/m/dictionary/audio/998.mp3"
mp3links = html.find_all('a', href=re.compile('.mp3$'))
for mp3 in mp3links:
    mp3 = "http://www.ancient-hebrew.org/m/dictionary/" + \
        mp3['href'].replace("\t", '')
    # print(mp3)

Я пытаюсь найти файлы изображений, текстовые файлы и аудиофайлы. И мой код в настоящее время находит все, кроме текста после </Font>. Например, я пытаюсь найти e-leph и eym и не знаю, как это сделать.

<A Name=    505 ></A>   <IMG SRC="../../files/heb-anc-sm-pey.jpg"><IMG SRC="../../files/heb-anc-sm-lamed.jpg"><IMG SRC="../../files/heb-anc-sm-aleph.jpg">   <Font face="arial" size="+1">  &#1488;&#1462;&#1500;&#1462;&#1507; </Font>     e-leph  <BR>    Thousand    <BR>    Ten times one hundred in amount or number.  <BR>Strong's Number:    505 <BR><A HREF="audio/ 505 .mp3"><IMG SRC="../../files/icon_audio.gif"  width="25" height="25" border="0"></A><BR> <A HREF=../ahlb/aleph.html#505><Font color=A50000><B>AHLB</B></Font></A>    <HR>
    <A Name=    517 ></A>   <IMG SRC="../../files/heb-anc-sm-mem.jpg"><IMG SRC="../../files/heb-anc-sm-aleph.jpg">   <Font face="arial" size="+1">  &#1488;&#1461;&#1501;   </Font>     eym <BR>    Mother  <BR>    A female parent. Maternal tenderness or affection. One who fulfills the role of a mother.   <BR>Strong's Number:    517 <BR><A HREF="audio/ 517 .mp3"><IMG SRC="../../files/icon_audio.gif"  width="25" height="25" border="0"></A><BR> <A HREF=../ahlb/aleph.html#517><Font color=A50000><B>AHLB</B></Font></A>    <HR>

Итак, в конце я хотел бы найти все слова, следующие за юникодом, такие как e-leph и eym

Ответы [ 2 ]

1 голос
/ 11 июня 2019

Вам не нужно регулярное выражение. Используйте next_sibling и селектор css, показанный ниже.

У вас есть шаблон глифов, затем шрифт, затем слово

enter image description here

Используйте комбинатор adjacent sibling, +, чтобы получить брата по тегу font, следующего сразу после тега img, как выделено на изображении выше. Тогда next_sibling приведет вас к слову.

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('http://www.ancient-hebrew.org/m/dictionary/1000.html')
soup = bs(r.content, 'lxml')
words = [item.next_sibling.strip() for item in soup.select('img + font')]

Пример вывода:

enter image description here

1 голос
/ 11 июня 2019

Если все наши желаемые выходные данные похожи на пример, приведенный в вопросе, мы можем, например, определить класс символов ([\w-]), добавить любые символы, которые нам нравится собирать, затем использовать <\/font> в качестве левой границы и <br> как правая граница. Мы также добавили бы группы с необязательными пробелами, и наше выражение выглядело бы так:

<\/font>(\s+)?([\w-]+?)(\s+)?<

или

<\/font>(\s+)?([\w-]+?)(\s+)?<br>

с флагом i.

Демо

Тест

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"<\/font>(\s+)?([\w-]+?)(\s+)?<"

test_str = ("<A Name=    505 ></A>   <IMG SRC=\"../../files/heb-anc-sm-pey.jpg\"><IMG SRC=\"../../files/heb-anc-sm-lamed.jpg\"><IMG SRC=\"../../files/heb-anc-sm-aleph.jpg\">   <Font face=\"arial\" size=\"+1\">  &#1488;&#1462;&#1500;&#1462;&#1507; </Font>     e-leph  <BR>    Thousand    <BR>    Ten times one hundred in amount or number.  <BR>Strong's Number:    505 <BR><A HREF=\"audio/ 505 .mp3\"><IMG SRC=\"../../files/icon_audio.gif\"  width=\"25\" height=\"25\" border=\"0\"></A><BR> <A HREF=../ahlb/aleph.html#505><Font color=A50000><B>AHLB</B></Font></A>    <HR>\n"
    "    <A Name=    517 ></A>   <IMG SRC=\"../../files/heb-anc-sm-mem.jpg\"><IMG SRC=\"../../files/heb-anc-sm-aleph.jpg\">   <Font face=\"arial\" size=\"+1\">  &#1488;&#1461;&#1501;   </Font>     eym <BR>    Mother  <BR>    A female parent. Maternal tenderness or affection. One who fulfills the role of a mother.   <BR>Strong's Number:    517 <BR><A HREF=\"audio/ 517 .mp3\"><IMG SRC=\"../../files/icon_audio.gif\"  width=\"25\" height=\"25\" border=\"0\"></A><BR> <A HREF=../ahlb/aleph.html#517><Font color=A50000><B>AHLB</B></Font></A>    <HR>\n")

matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

RegEx

Если это выражение нежелательно или вы хотите изменить его, посетите regex101.com .

RegEx Circuit

jex.im визуализирует регулярные выражения:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...