Я пытаюсь очистить городской словарь с помощью 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', "Franglais");">
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', "Strine");">
Strine
</a>
, Kiwi,
<a class="autolink" href="/define.php?term=Estuary" onclick="ga('send', 'event', 'Autolink', 'Click', "Estuary");">
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', "Franglais");">Franglais</a<br/>Acccents include Canadian - which might be boring, <a class="autolink" href="/define.php?term=Strine" onclick="ga('send', 'event', 'Autolink', 'Click', "Strine");">Strine</a>, Kiwi, <a class="autolink" href="/define.php?term=Estuary" onclick="ga('send', 'event', 'Autolink', 'Click', "Estuary");">Estuary</a>,<br/>- this is known as geordie</div>mpediment
Как видите, есть несоответствие. Мне действительно было интересно, почему это происходит?