Coder Social home page Coder Social logo

digipodium / dputils Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 3.0 2.81 MB

open source library from digipodium

Home Page: https://digipodium.github.io/dputils/

License: MIT License

Python 100.00%
beautifulsoup datacollection file file-handling-in-python python python3 requests-module scraper webscraping read-files

dputils's Introduction

Python Version Contributions welcome License Build Status

Documentation Status PyPI - Downloads Stars

A python library which can be used to extraxct data from files, pdfs, doc(x) files, as well as save data into these files. This library can be used to scrape and extract webpage data from websites as well.

Installation Requirements and Instructions

Python versions 3.8 or above should be installed. After that open your terminal: For Windows users:

pip install dputils

For Mac/Linux users:

pip3 install dputils

Files Module

Functions from dputils.files: for now, the files module has two functions:

  1. get_data:

    • To import, use statement:

      from dputils.files import get_data
    • Obtains data from files of any extension given as args(supports text files, binary files, pdf, doc for now, more coming!)

    • sample call:

      content = get_data(r"sample.docx")
      print(content)
    • Returns a string or binary data depending on the output arg

    • images will not be extracted

  2. save_data:

    • save_data can be used to write and save data into a file of valid extension.
    • sample call:
      from dputils.files import save_data
      
      pdfContent = save_data("sample.pdf", "Sample text to insert")
      print(pdfContent)
    • Returns True if file is successfully accessed and modified. Otherwise, False.

Scrape Module

Data extraction from a page

Here's a basic tutorial to help you get started with the scraper module.

  1. Import the required classes and functions:
from dputils.scrape import Scraper, Tag
  1. Initialize the Scraper class with the URL of the webpage you want to scrape:
url = "https://www.example.com"
scraper = Scraper(url)
  1. Define the tags you want to scrape using the Tag class:
title_tag = Tag(name='h1', cls='title', output='text')
price_tag = Tag(name='span', cls='price', output='text')
  1. Extract data from the page:
data = scraper.get_data_from_page(title=title_tag, price=price_tag)
print(data)

Extracting list of items from a page

For more advanced usage, such as extracting repeated data from lists of items on a page, you can use the following approach:

  1. Initialize the Scraper class:
url = "https://www.example.com/products"
scraper = Scraper(url)
  1. Define the tags for the target section and the items within that section: For repeated data extraction, you need to define Target and item and pass it to get_repeating_data_from_page() method.
    • target - defines the Tag() for area of the page containing the list of items.
    • items - defines the Tag() for repeated items within the target section. Like a product-card in product grid/list.
target_tag = Tag(name='div', cls='product-list')
item_tag = Tag(name='div', cls='product-item')
title_tag = Tag(name='h2', cls='product-title', output='text')
price_tag = Tag(name='span', cls='product-price', output='text')
link_tag = Tag(name='a', cls='product-link', output='href')
  1. Extract repeated data from the page:
products = scraper.get_repeating_data_from_page(
    target=target_tag,
    items=item_tag,
    title=title_tag,
    price=price_tag,
    link=link_tag
)
for product in products:
    print(product)

These functions can used on python versions 3.8 or greater.

References for more help: https://digipodium.github.io/dputils/

Contribution

if you want to contribute to this project and make it better, your help is very welcome.

  • Fork the project
  • Create your feature branch (git checkout -b feature/fooBar)
  • Commit your changes (git commit -am 'Add some fooBar')
  • Push to the branch (git push origin feature/fooBar)
  • Create a new Pull Request
  • Wait for your PR to be reviewed and merged
  • Star the project if you've found it useful
  • Share the project with your friends
  • Create an issue if you find a bug or want to request a new feature
  • Improve the project by refactoring the code
  • Review the PRs of other contributors
  • Suggest new features
  • Suggest new technologies to be used

Thank you for using dputils!

dputils's People

Contributors

akuls1008 avatar helloharendra avatar imgbotapp avatar zaid-kamil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dputils's Issues

update documentation

Documents need to be updated. Please update it ASAP

The libraries are updated, Please Updated the Documents related to it

Screenshot

Screenshot 2024-05-16 104406
Screenshot 2024-05-16 104646

Could not save data in correct format in the PDF file

import os
data = ""
folder = 'C:/windows/system32/'
files  = os.listdir(folder)
for item in files:
    fullpath = os.path.join(folder,item)
    if os.path.isfile(fullpath):
        sz = os.path.getsize(fullpath)
        out = f'{item.ljust(70)} | {sz/1024 :.1f} kb'
        data += out + '\n'

then

from dputils import files
files.save_data('dlls.pdf',data)

generated a PDF files but it does not contain correct data
image

Could not Store info in Docx file

i tested this code

import os
data = ""
folder = 'C:/windows/system32/'
files  = os.listdir(folder)
for item in files:
    fullpath = os.path.join(folder,item)
    if os.path.isfile(fullpath):
        sz = os.path.getsize(fullpath)
        out = f'{item.ljust(70)} | {sz/1024 :.1f} kb'
        data += out + '\n'

and then

from dputils import files
files.save_data('dlls.docx',data)

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.