Обновлённ пример кода с Pars
This commit is contained in:
+11
-4
@@ -34,23 +34,25 @@ for index, url in enumerate(urlList):
|
||||
|
||||
- The **mkdir** method is used to create a folder with the name _nameDir_ if it does not already exist.
|
||||
|
||||
### Example Parser Using ipars:
|
||||
### Example Using the Pars Class:
|
||||
|
||||
```py
|
||||
# About the ProgressBarManager class, read below
|
||||
from ipars import Pars, ProgressBarManager
|
||||
p = Pars()
|
||||
nameFile = 'index.html'
|
||||
nameFolder = 'img'
|
||||
|
||||
# Getting the HTML page
|
||||
p.getDynamicPage(nameFile, 'https://duckduckgo.com/?q=теплица+социальных+технологий+youtube&iar=videos&atb=v454-1', closeWindow=0)
|
||||
p.getDynamicPage(nameFile, 'https://duckduckgo.com/?q=теплица+социальных+технологий+youtube&iar=videos&atb=v454-1', closeWindow=False, timeSleep=5)
|
||||
|
||||
# Getting the BeautifulSoup Object
|
||||
soup = p.returnBs4Object(nameFile)
|
||||
|
||||
# Finding all answer cards
|
||||
# The first results are what we wanted, and the rest are not. Therefore, we prefer to get the first 84 elements
|
||||
allCards = soup.find*all(class*='b_NgmZrVnRtV8MZMEjLs')[:84]
|
||||
locator = 'b_NgmZrVnRtV8MZMEjLs'
|
||||
allCards = soup.find_all(class_=locator)[:84]
|
||||
|
||||
# Getting all images
|
||||
allImg = [card.find('img') for card in allCards]
|
||||
@@ -59,7 +61,6 @@ allImg = [card.find('img') for card in allCards]
|
||||
allSrc = p.getAttributes(allImg, 'src')
|
||||
|
||||
# Creating a folder img if it does not already exist
|
||||
nameFolder = 'img'
|
||||
p.mkdir(nameFolder)
|
||||
|
||||
# Creating a ProgressBarManager object
|
||||
@@ -71,6 +72,12 @@ url = 'https:' + url
|
||||
p.getStaticPage(f'./{nameFolder}/img{index}.png', url, writeMethod='wb')
|
||||
bar.next()
|
||||
bar.finish()
|
||||
|
||||
# Let's see what appeared in the img folder
|
||||
p.pprint(p.listdir(nameFolder))
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
### Example Usage of _getAttributes_ and _getTexts_ Methods
|
||||
|
||||
+19
-4
@@ -34,23 +34,33 @@ for index, url in enumerate(urlList):
|
||||
|
||||
- Метод **mkdir** используется для создания папки с именем _nameDir_ если она ещё не существует
|
||||
|
||||
### Пример парсера с использованием ipars:
|
||||
- Метод **listdir** используется для получения списка файлов в указанной папке
|
||||
|
||||
### Пример использования класса Pars:
|
||||
|
||||
```py
|
||||
# О классе ProgressBarManager читай ниже
|
||||
from ipars import Pars, ProgressBarManager
|
||||
p = Pars()
|
||||
nameFile = 'index.html'
|
||||
nameFolder = 'img'
|
||||
|
||||
def main():
|
||||
# Получаем html страницу
|
||||
p.getDynamicPage(nameFile, 'https://duckduckgo.com/?q=теплица+социальных+технологий+youtube&iar=videos&atb=v454-1', closeWindow=0)
|
||||
p.getDynamicPage(nameFile, 'https://duckduckgo.com/?q=теплица+социальных+технологий+youtube&iar=videos&atb=v454-1', closeWindow=False, timeSleep=5)
|
||||
|
||||
# Получаем объект BautifullSoup
|
||||
soup = p.returnBs4Object(nameFile)
|
||||
|
||||
# Находим все карточки ответов
|
||||
# Первые результаты выдачи те что хотелось получить, а остальные нет. Поэтому нам желательно получить первые 84 элемента
|
||||
allCards = soup.find_all(class_='b_NgmZrVnRtV8MZMEjLs')[:84]
|
||||
locator = 'b_NgmZrVnRtV8MZMEjLs'
|
||||
allCards = soup.find_all(class_=locator)[:84]
|
||||
if len(allCards) == 0:
|
||||
print(f'''Something went wrong. Here is a list of possible causes:
|
||||
1) DuckDuckGo has changed the class for video cards. The code uses the locator "{locator}"
|
||||
2) The site did not load. Try increasing the timeSleep parameter or make a request later.''')
|
||||
return
|
||||
|
||||
# Получаем все изображения
|
||||
allImg = [card.find('img') for card in allCards]
|
||||
@@ -59,7 +69,6 @@ allImg = [card.find('img') for card in allCards]
|
||||
allSrc = p.getAttributes(allImg, 'src')
|
||||
|
||||
# Создаём папку img если её ещё нет
|
||||
nameFolder = 'img'
|
||||
p.mkdir(nameFolder)
|
||||
|
||||
# Создаём объект ProgressBarManager
|
||||
@@ -71,6 +80,12 @@ for index, url in enumerate(allSrc):
|
||||
p.getStaticPage(f'./{nameFolder}/img{index}.png', url, writeMethod='wb')
|
||||
bar.next()
|
||||
bar.finish()
|
||||
|
||||
# Смотрим что появилось в папке img
|
||||
p.pprint(p.listdir(nameFolder))
|
||||
|
||||
if __name__=='__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
### Пример использования методов _getAttributes_ и _getTexts_
|
||||
|
||||
Reference in New Issue
Block a user