Skip to main content

Convert bs4 Tags into Json

Project description

PyPi version PyPi pyversions PyPi license

Convert HTML Tags of BeautifulSoup class to JSON data.

Installation

This package is available on PyPi. Just use pip install -U bs2json to install it. Then you can import it using from bs2json import bs2json.

Example Syntax

from bs4 import BeautifulSoup
from requests import get
from bs2json import bs2json

html = get('https://ijazurrahim.com').text

soup = BeautifulSoup(html,'lxml')
converter = bs2json()

tags = soup.findAll('a')
json = converter.convertAll(tags,join=True)
print(json)

Output

Upon running the Above Program, you will get the following output

[{'a': {'i': {'attributes': {'class': ['fa', 'fa-facebook']}}, 'attributes': {'href': 'https://web.facebook.com/MisterDebugger'}}}, {'a': {'i': {'attributes': {'class': ['fa', 'fa-instagram']}}, 'attributes': {'href': 'https://www.instagram.com/MisterDebugger'}}}, {'a': {'i': {'attributes': {'class': ['fa', 'fa-twitter']}}, 'attributes': {'href': 'https://www.twitter.com/muibraheem96'}}}, {'a': {'i': {'attributes': {'class': ['fa', 'fa-stack-overflow']}}, 'attributes': {'href': 'https://www.stackoverflow.com/users/9140224/ijaz-ur-rahim'}}}, {'a': {'i': {'attributes': {'class': ['fa', 'fa-linkedin']}}, 'attributes': {'href': 'https://www.linkedin.com/in/muibraheem96'}}}, {'a': {'i': {'attributes': {'class': ['fa', 'fa-github']}}, 'attributes': {'href': 'https://www.github.com/MrDebugger'}}}, {'a': {'i': {'attributes': {'class': ['fa', 'fa-building']}}, 'attributes': {'href': 'https://www.upwork.com/freelancers/~01c44a17a8ed828883'}}}, {'a': {'i': {'attributes': {'class': ['fa', 'fa-youtube']}}, 'attributes': {'href': 'https://www.youtube.com/ijazurrahim'}}}]

Other Methods

  • There are total 3 methods labels(), convert() and convertAll() where convert() and convertAll() takes two parameters of type bs4.element.Tag, dict (optional) and three parameters of type bs4.ResultSet, list (optional), bool (default: False) respectively.
  • labels() method takes two positional arguments attributes and text which replaces the respective names in output json.
  • convert() method takes element (bs4.element.Tag) and json (dict: optional) as arguments. Whereas element is result of BeautifulSoup().find() and json is an empty dictionary or already constructed dictionary.
  • convertAll() method takes elements (bs4.element.Tag), List (list: optional) and join (bool: default=False) as arguments. Whereas elements is result of BeautifulSoup().findAll(), list is an empty list or already constructed list and join is a boolean value. When join becomes True, same tags will append to one list and assinged to one tag name resulting a dictionary. For more information, see example files.

Contributing

We appreciate all contributions. If you are planning to contribute any bug-fixes, please do so without further discussions.

If you plan to contribute new features etc. please first open an issue or reuse an exisiting issue, and discuss the feature with us. We will discuss with you on the issue timely or set up conference calls if needed.

We appreciate all contributions and thank all the contributors!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bs2json-0.0.3.tar.gz (4.3 kB view details)

Uploaded Source

Built Distributions

bs2json-0.0.3-py3.8.egg (10.5 kB view details)

Uploaded Source

bs2json-0.0.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file bs2json-0.0.3.tar.gz.

File metadata

  • Download URL: bs2json-0.0.3.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.7

File hashes

Hashes for bs2json-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a7cd26edd676f1978c1ce7f5a3a1e6c8d724e3144b1b0b9ecf58e1bda74a533d
MD5 b441b801aecdc86a7d73d88135691b43
BLAKE2b-256 eeced6c9cce48965172ebb84d7e4757758f40ee1efb88989db07e615e59ccf15

See more details on using hashes here.

File details

Details for the file bs2json-0.0.3-py3.8.egg.

File metadata

  • Download URL: bs2json-0.0.3-py3.8.egg
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for bs2json-0.0.3-py3.8.egg
Algorithm Hash digest
SHA256 c273e1b0f348843c4e04994bfb327ee7c35156bcbb350202474d033ad6264002
MD5 0346676d701c2e80a788ace2eaa5899a
BLAKE2b-256 8ae012e53170ed661ce2540e8497c72981f0be00b131f55a6df47c5297416e85

See more details on using hashes here.

File details

Details for the file bs2json-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: bs2json-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.7

File hashes

Hashes for bs2json-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 10b8fd95906b54519076fa307cd0e7a83db5403033cedf1c3f4d9ca9b3221be3
MD5 df69381084d4a3898c5664325a238250
BLAKE2b-256 f760f9c4b45e01b698ef8bafb9b8bead5747f2707d593e9067d0f192ffb56c8f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page