Nano Hash - криптовалюты, майнинг, программирование

Beautiful Soup не дает фактический тег div

Я пытаюсь очистить городской словарь с помощью Python . Но в настоящее время я сталкиваюсь с некоторыми проблемами.

Сначала я решил очистить

‹div class = def-panel...

теги div, которые содержат информацию о слове. Например, смысл, примеры, участник... И, кстати, эти теги div def-panel находятся в

‹идентификатор div = содержание...

или XPath

//*[@id=content]

Это мой класс для простых операций на этом сайте.

import requests
import urllib.request
from bs4 import BeautifulSoup
class  UrbanDict:
    URL = "https://www.urbandictionary.com/"
    search_form = "define.php?term={}"
    def __init__(self):
        pass

    def get_soup_response(self,link):
        response = requests.get(link)
        soup = BeautifulSoup(response.text,"html.parser")

        return soup


    def search(self,word):
        soup = self.get_soup_response(self.URL+self.search_form.format(word))
        return soup

И это мой код, пока я тестирую класс UrbanDict.

if __name__ == "__main__":
    urban = UrbanDict() # Creating Object
    soup = urban.search("world") # Getting the page of "world" word . 
    defpanels = soup.find("div",{"id":"content"}).findAll("div",{"class":"def-panel"}) # Getting the panel divs . 
    for defpanel in defpanels: # Iterating panel divs
        word = defpanel.find("div",{"class" : "def-header"}).text # Checking the div is the correct div
        if word.lower() == "world" :
            print("="*64)
            meaning_div = defpanel.find("div",{"class":"meaning"})#getting meaning div of word
            example_div = defpanel.find("div",{"class":"example"})#getting example div  of word

            print(meaning_div)

Когда я печатаю div, я вижу некоторое несоответствие. Код div без prettify отличается от кода с prettify . Код, который я написал для управления этим:

if __name__ == "__main__":
    urban = UrbanDict() # Creating Object
    soup = urban.search("world") # Getting the page of "world" word . 
    defpanels = soup.find("div",{"id":"content"}).findAll("div",{"class":"def-panel"}) # Getting the panel divs . 
    print("Prettify used \n")

    print(defpanels[2].find("div",{"class" : "meaning"}).prettify(encoding="utf-8").decode("utf-8"))

    print("="*48)
    print("\nPrettify NOT used \n")

    print(defpanels[2].find("div",{"class" : "meaning"}))

И вывод:

Prettify used 

<div class="meaning">
 A language, derived from English (or English-English, American-English etc. etc. ad nauseam).
 <br/>
 This is the de facto language of international commerce, finance, shipping, aviation, the web, etc.
 <br/>
 It has many dialects.
 <br/>
 Chinglish, Singlish,
 <a class="autolink" href="/define.php?term=Franglais" onclick="ga('send', 'event', 'Autolink', 'Click', &quot;Franglais&quot;);">
  Franglais
 </a>
 and Spanglish spring to mind.
 <br/>
 Acccents include Canadian - which might be boring,
 <a class="autolink" href="/define.php?term=Strine" onclick="ga('send', 'event', 'Autolink', 'Click', &quot;Strine&quot;);">
  Strine
 </a>
 , Kiwi,
 <a class="autolink" href="/define.php?term=Estuary" onclick="ga('send', 'event', 'Autolink', 'Click', &quot;Estuary&quot;);">
  Estuary
 </a>
 , Scouse, Cockney and Hindglish.
 <br/>
 There is one recognised speech impediment
 <br/>
 - this is known as geordie
</div>

================================================

Prettify NOT used 

<br/>Chinglish, Singlish, <a class="autolink" href="/define.php?term=Franglais" onclick="ga('send', 'event', 'Autolink', 'Click', &quot;Franglais&quot;);">Franglais</a<br/>Acccents include Canadian - which might be boring, <a class="autolink" href="/define.php?term=Strine" onclick="ga('send', 'event', 'Autolink', 'Click', &quot;Strine&quot;);">Strine</a>, Kiwi, <a class="autolink" href="/define.php?term=Estuary" onclick="ga('send', 'event', 'Autolink', 'Click', &quot;Estuary&quot;);">Estuary</a>,<br/>- this is known as geordie</div>mpediment

Как видите, есть несоответствие. Мне действительно было интересно, почему это происходит?


  • Попробуйте сохранить веб-страницу в формате HTML, чтобы проверить результат, который вы получаете. Часто веб-сайты ограничивают доступ, меняют области просмотра, запрашивают повторную проверку или устанавливают приманки для захвата веб-краулеров. Проверка вывода может дать вам некоторые подсказки о том, что происходит. 20.07.2020

Ответы:


1

Проблема в используемом парсере. html.parser и lxml неправильно анализируют найденные на странице теги. Используйте html5lib для получения наилучших результатов:

import requests
from bs4 import BeautifulSoup


term = 'world'
url = 'https://www.urbandictionary.com/define.php'
soup = BeautifulSoup(requests.get(url, params={'term': term}).content, 'html5lib')  # <-- use html5lib

for r, m, e in zip(soup.select('.ribbon'),
                   soup.select('.meaning'),
                   soup.select('.example')):
    if 'Word of the Day' in r.text:
        continue
    print(m.text)
    print()
    print(e.text)
    print('-' * 120)

Отпечатки:

the f***d off place where we live

violence, death, corruption- tis the world we live in
------------------------------------------------------------------------------------------------------------------------
A language, derived from English (or English-English, American-English etc. etc. ad nauseam).
This is the de facto language of international commerce, finance, shipping, aviation, the web, etc.
It has many dialects. 
Chinglish, Singlish, Franglais and Spanglish spring to mind.
Acccents include Canadian - which might be boring, Strine, Kiwi, Estuary, Scouse, Cockney and Hindglish.
There is one recognised speech impediment
- this is known as geordie

If you understand this, you understand World.
------------------------------------------------------------------------------------------------------------------------
A word that needs to be defined. Basically its a sphere, floating in space. It has both land and Sea...oh and some air too. it is also called earth. Its pretty tight.

Urban Dictionary. Define your world.
------------------------------------------------------------------------------------------------------------------------
What I rocked last night.

Yeah, he rocked my world.
------------------------------------------------------------------------------------------------------------------------
An alternate term for Earth, the planet we live on, the third from the sun.

Generally, "world" does not describe the physical planet, but rather the community within it.

The world is starting to lose it...
All around the world, people have McDonalds.
What a wonderful world.
------------------------------------------------------------------------------------------------------------------------
a messed up circle where a bunch of crazy kids live. people from marz jupiter venus ect. like to call these crazy kids humans.

"so, where do you live?"
"i live in that circle looking thing, we name that fucked up place the world"
"oh dear"
------------------------------------------------------------------------------------------------------------------------
20.07.2020
Новые материалы

Кластеризация: более глубокий взгляд
Кластеризация — это метод обучения без учителя, в котором мы пытаемся найти группы в наборе данных на основе некоторых известных или неизвестных свойств, которые могут существовать. Независимо от..

Как написать эффективное резюме
Предложения по дизайну и макету, чтобы представить себя профессионально Вам не позвонили на собеседование после того, как вы несколько раз подали заявку на работу своей мечты? У вас может..

Частный метод Python: улучшение инкапсуляции и безопасности
Введение Python — универсальный и мощный язык программирования, известный своей простотой и удобством использования. Одной из ключевых особенностей, отличающих Python от других языков, является..

Как я автоматизирую тестирование с помощью Jest
Шутка для победы, когда дело касается автоматизации тестирования Одной очень важной частью разработки программного обеспечения является автоматизация тестирования, поскольку она создает..

Работа с векторными символическими архитектурами, часть 4 (искусственный интеллект)
Hyperseed: неконтролируемое обучение с векторными символическими архитектурами (arXiv) Автор: Евгений Осипов , Сачин Кахавала , Диланта Хапутантри , Тимал Кемпития , Дасвин Де Сильва ,..

Понимание расстояния Вассерштейна: мощная метрика в машинном обучении
В обширной области машинного обучения часто возникает необходимость сравнивать и измерять различия между распределениями вероятностей. Традиционные метрики расстояния, такие как евклидово..

Обеспечение масштабируемости LLM: облачный анализ с помощью AWS Fargate и Copilot
В динамичной области искусственного интеллекта все большее распространение получают модели больших языков (LLM). Они жизненно важны для различных приложений, таких как интеллектуальные..