first commit

This commit is contained in:
Ilia Miheev
2025-10-21 21:09:30 +03:00
commit 17eeb0ccb8
25 changed files with 808 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
### Working with CsvManager
This class is designed to write and extract information from csv files
The CsvManager class takes three arguments: the newline character newline (default is an empty string), the encoding of the opened files encoding (default is UTF-8), and the delimiter used in the CSV file delimiter (default is ";").
```py
from ipars import CsvManager
c = CsvManager()
```
### Brief Overview of CsvManager Methods
1. The **writerow** method writes a row to the CSV file. The method takes the path to the CSV file, the write mode, and a list of data that will be written to the file's row.
2. The **writerows** method takes the same arguments as writerow, but the row must be a double list with data for writing. The difference between these methods is that writerow writes one row, while writerows writes as many as there are in the double list.
3. The **getRows** method is used to retrieve a list of rows in the CSV file. The method takes the path to the file from which the rows will be obtained.
4. The **pprint** method is the same as that of Pars.
### Example of Using CsvManager
```py
from ipars import CsvManager
c = CsvManager()
# writing headers
writer = c.writerow('./data.csv', 'w', ['Quantity', 'Price', 'Total'])
# writing data
writer = c.writerows('./data.csv', 'a', [
["5", "5", "25"],
["6", "6", "36"],
["7", "7", "49"],
])
# retrieving rows from the table
rows = c.getRows('./data.csv')
# printing table rows
c.pprint(rows)
```
+34
View File
@@ -0,0 +1,34 @@
### Working with JsonManager
This class is designed to write and extract information from json files.
JsonManager takes only one argument — the encoding in which the files will be read. The default is UTF-8.
```py
from ipars import JsonManager
j = JsonManager()
```
### Brief Overview of JsonManager Methods
1. The **load** method is used to retrieve data from a JSON file at the specified path.
2. The **dump** method is used to write data to a JSON file. It takes the path to the file and the data to be written.
3. The **pprint** method is the same as that of Pars.
### Example of Using JsonManager
```py
from ipars import JsonManager
j = JsonManager()
# Writing data
j.dump('./data.json', [1, 2, 3, 4, 5, 6, 7])
# Retrieving data
data = j.load('./data.json')
j.pprint(data) # [1, 2, 3, 4, 5, 6, 7]
```
+78
View File
@@ -0,0 +1,78 @@
### Working with Pars
This class is designed to work with requests and the resulting html page.
The Pars class does not take any arguments for its constructors.
```python
from ipars import Pars
p = Pars()
```
### Brief Overview of Pars Methods
1. The **getStaticPage** method takes the URL of the page, the path where the page will be saved, the write method, and request headers. The "wb" write method is used for saving images; by default, the write method is set to "w", which is used for HTML pages. If request headers are not specified, random user-agent headers will be used. The method returns the status response from the website, which should be used for conducting checks.
2. The **getDynamicPage** method uses the Selenium library to get a dynamically updated page. This is useful when content on the page loads dynamically. It takes the URL of the page, the save path, closeWindow, and timeSleep. By default, the Selenium browser opens in the background, and its operation is not visible, but if closeWindow is set to False, the code execution process will be shown. The timeSleep parameter can extend the page load time if content takes long to load.
3. The **gpsa** (get page semi-automatically) method is similar to the getDynamicPage method but works in a semi-automatic mode. It opens the webpage and waits until Enter is pressed in the terminal. During this time, you can register on the site and/or switch to the desired tab on the site, after which pressing Enter will allow the method to parse the page. This is suitable for social media feeds where unauthorized users have limited access to content. It accepts the same arguments and serves the same purposes as getDynamicPage, except for closeWindow.
4. The **returnBs4Object** method returns a BeautifulSoup object. It takes the path to the HTML page, converts its contents into a BeautifulSoup object, the file open encoding (default is UTF-8), and the parser type (default is lxml).
5. The **getAttributes** method is used to retrieve a list of attributes from a list of BeautifulSoup objects. It takes a list of BeautifulSoup objects and the name of the attribute to be extracted from the elements of the list.
6. The **getTexts** method is used to retrieve a list of text from a list of BeautifulSoup objects. It takes a list of BeautifulSoup objects and the needFix parameter. If this parameter is set to True, \n, \t, and spaces from the ends will be removed from the text.
7. The **pprint** method is used for displaying variable values that have a high level of nesting. For example, if you have an array of objects, where another array of objects is used as the value of a key.
8. The **mkdir** method is used to create a directory with the name nameDir if it does not already exist.
### Example Parser Using ipars:
```py
# Read about the ProgressBarManager class below
from ipars import Pars, ProgressBarManager
p = Pars()
nameFile = 'index.html'
# Getting the HTML page
p.getDynamicPage(nameFile, 'https://duckduckgo.com/?q=greenhouse+social+technologies+youtube&iar=videos&atb=v454-1', closeWindow=0)
# Getting the BeautifulSoup object
soup = p.returnBs4Object(nameFile)
# Finding all answer cards
# The first results are those we want, while the rest are not. Therefore, we want the first 84 elements
allCards = soup.find*all(class*='b_NgmZrVnRtV8MZMEjLs')[:84]
# Getting all images
allImg = [card.find('img') for card in allCards]
# Getting all links
allSrc = p.getAttributes(allImg, 'src')
# Creating the img folder if it doesn't already exist
nameFolder = 'img'
p.mkdir(nameFolder)
# Creating a ProgressBarManager object
bar = ProgressBarManager(len(allSrc))
# Downloading images
for index, url in enumerate(allSrc):
url = 'https:' + url
p.getStaticPage(f'./{nameFolder}/img{index}.png', url, writeMethod='wb')
bar.next()
bar.finish()
```
### Example Using getAttributes and getTexts Methods
```py
from ipars import Pars
p = Pars()
p.getStaticPage('./index.html', 'https://google.com')
soup = p.returnBs4Object('./index.html')
allTegA = soup.find_all('a')
a1 = p.getTexts(allTegA, needFix=1)
p.pprint(a1)
a2 = p.getAttributes(allTegA, 'href')
p.pprint(a2)
```
+66
View File
@@ -0,0 +1,66 @@
### Working with ProgressBarManager
The class creates a progress bar for better visibility of code execution. It takes five arguments:
1. **max**: a required parameter that indicates the maximum number of iterations in the progress bar.
2. **message**: a message displayed before the progress bar.
3. **color**: the color of the progress bar.
4. **fill**: a character used to fill the completed portion.
5. **width**: the size of the progress bar in characters.
```py
from ipars import ProgressBarManager
bar = ProgressBarManager(100) # Here max is set to 100
```
### Brief Overview of ProgressBarManager Methods
1. The **next** method starts the next iteration of the progress bar.
2. The **finish** method completes the progress bar.
### Example of Using ProgressBarManager
```py
# Importing libraries
from ipars import ProgressBarManager
from time import sleep
maxValue = 300
# Creating a default progress bar
bar = ProgressBarManager(maxValue)
# Simulating work
for _ in range(maxValue // 2):
sleep(0.1)
bar.next()
# Turning off the progress bar
bar.finish()
# Creating a more customized progress bar
bar = ProgressBarManager(
maxValue,
message='Downloading process',
color='red',
fill='\*',
width=50
)
# Simulating work
for _ in range(maxValue):
sleep(0.1)
bar.next()
# Turning off the progress bar
bar.finish()
```
+27
View File
@@ -0,0 +1,27 @@
### Library for File Handling During Parsing {docsify-ignore-all}
During parsing, it is often necessary to download HTML pages and work with JSON and CSV files. This library is designed to simplify the coding tasks for such activities and provides a range of additional features.
### The library contains three primary classes
1. **Pars** for handling requests and bs4.
2. **JsonManager** for working with JSON.
3. **CsvManager** for handling CSV files.
### There are also three auxiliary classes
1. **ProgressBarManager** for creating progress bars.
2. **TimerManager** for measuring the execution time of specific code.
3. **ZipManager** for archiving files and folders.
To install the library:
```bash
pip install ipars
```
or
```bash
pip3 install ipars
```
+35
View File
@@ -0,0 +1,35 @@
### Working with TimerManager
The TimerManager class is designed to track the execution time of code. It allows you to obtain the total runtime in various formats.
```py
from ipars import TimerManager
t = TimerManager()
```
### Brief Overview of TimerManager Methods
1. The **start** method marks the starting point of the time measurement.
2. The **end** method marks the endpoint of the time measurement.
3. The **getWorkTime** method returns the total execution time in the specified format. Supported formats are seconds, minutes, and hours. The ndigits parameter specifies the number of decimal places for rounding; by default, the number is not rounded.
### Example of Using TimerManager
```py
from time import sleep
from ipars import TimerManager
t = TimerManager()
# Starting the timer
t.start()
sleep(2) # simulating two seconds of work
t.end()
# Displaying results in different formats
print(f"Execution time in seconds: {t.getWorkTime()}") # 2.0008320808410645
print(f"Execution time in minutes: {t.getWorkTime(ndigits=2, format='minutes')}") # 0.03
print(f"Execution time in hours: {t.getWorkTime(ndigits=4, format='hours')}") # 0.0006
```
+33
View File
@@ -0,0 +1,33 @@
### Working with ZipManager
The ZipManager class is used for archiving a folder of files. It takes only one argument — the compression level for the files.
'none' => No compression
'normal' => Normal compression
'hard' => Increased compression
'maximum' => Maximum compression
```py
from ipars import ZipManager
z = ZipManager()
```
### Brief Overview of ZipManager Methods
1. The **zip_file** method is used to archive a single file. It takes the path to the source file and the path to the output file.
2. The **zip_folder** method is used to archive a directory. It takes the path to the source directory and the path to the output file.
### Example of Using ZipManager
```py
from ipars import ZipManager
z = ZipManager(compression='maximum')
z.zip_file('./your_file.txt', 'file_archive_maximum.zip')
z.zip_folder('./your_folder/', 'folder_archive_maximum.zip')
```
+15
View File
@@ -0,0 +1,15 @@
<!--_sidebar.md-->
* [**Home**](./pages/en/README_EN.md)
* [**Pars**](./pages/en/Pars_EN.md)
* [**JsonManager**](./pages/en/JsonManager_EN.md)
* [**CsvManager**](./pages/en/CsvManager_EN.md)
* [**ProgressBar**](./pages/en/ProgressBarManager_EN.md)
* [**TimerManager**](./pages/en/TimerManager_EN.md)
* [**ZipManager**](./pages/en/ZipManager_EN.md)