Python Forum
Python Obstacles | Jeet-Kune-Do | BS4 (Tags > MariaDB) [URL/Local HTML]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Obstacles | Jeet-Kune-Do | BS4 (Tags > MariaDB) [URL/Local HTML]
#1
Question 
Python Obstacles | Jeet-Kune-Do | BS4 (Tags > MariaDB) [URL/Local HTML]

I am working on solving my Thread: "Securing King James Bible (KJV) + King James Bible 1611 (KJV1611) in MariaDB/MySQL"

Source Blogs/Tutorials:
https://itsmycode.com/python-urllib-erro...forbidden/ (Helped Resolve 403 Forbidden & Taught me how to implement User-Agent Spoofing within my code).

I am looking to achieve the following and this is my progress so far!


A.1) URL > MariaDB using PyMySQL and BeautifulSoup4 (Single File = Single Row Parsed) [Currently Working]

Database Name: HolyBible
Table Name: Jeet-Kune-Do-A

Database Schematics as Follows:

Column #1: h1_text | Datatype: TEXT | Null
Column #2: p_text | Datatype: TEXT | Null
Working Python 3.9 Code is as Follows:

# 06_Jeet-Kune-Do.A
# 02/07/2022 
# Python 3.9.9
# Objective: Parse a Single HTML File using Beautiful Soup 4 and PyMySQL and Insert to MariaDB
# Target: kingsjamesbibleonline.org
# Securing God's Word in MariaDB for Sharpen Your Sword Ministries 
# Source 1: https://itsmycode.com/python-urllib-error-httperror-http-error-403-forbidden/ | User-Agent Spoofing
# python-forum.io Thread URL: https://python-forum.io/thread-36325-post-153148.html

import urllib.request
import pymysql
import pymysql.cursors
from bs4 import BeautifulSoup

# User-Agent Spoofing - urllib Request
from urllib.request import Request, urlopen
req = Request('https://www.kingjamesbibleonline.org/Matthew-24-24/', headers={'User-Agent': 'Mozilla/5.0'})
html = urlopen(req).read()
#html = urlopen(req,timeout=10).read()

# Assign a Python Variable to Beautiful Soup 4's HTML Parser
htmlParse = BeautifulSoup(html, 'html.parser')

# Select HTML Element for Data Parsing #1
htmlParse.find_all("h1")[0].get_text()

# Select HTML Element for Data Parsing #2
htmlParse.find_all("p")[0].get_text()

# Connection to MariaDB 10.5.x with a Database selected using PyMySQL
connection = pymysql.connect(host='localhost',
                 user='username',
                 password='password',
                 db='HolyBible',
                 charset='utf8mb4',
                 cursorclass=pymysql.cursors.DictCursor)

# Assign a Variable to BeautifulSoup4 Parsing using soup.find_all("") function 
# which is telling BeautifulSoup4 to find all <h1> & <p> tags
# and store tags signified as zero [0]  and then 
# strips the tags using soup.find_all("h1").get_text() & soup.find_all("p").get_text() 
# leaving us only the text to pass to MariaDB for storage
h1_text = htmlParse.find_all("h1")[0].get_text()
p_text = htmlParse.find_all("p")[0].get_text()

try: 
    with connection.cursor() as cursor: 
            sql = "INSERT INTO `Jeet-Kune-Do-A` (`h1_text`,`p_text`) VALUES (%s, %s)" 
            cursor.execute(sql, (h1_text,p_text)) 

    connection.commit() 
finally:
    connection.close()

# Alert Brandon | Payload Delivered Successfully!
print ("Python Script Executed && Payload Delievered Successfully!")
Output after Running 06_Jeet-Kune-Do.A.py:

root@FireDragon:/home/brandon/Python/06_Jeet-Kune-Do/Part.A.Single.HTML.File.2.MariaDB# python3.9 06_Jeet-Kune-Do.A.py 
Python Script Executed && Payload Delievered Successfully!
root@FireDragon:/home/brandon/Python/06_Jeet-Kune-Do/Part.A.Single.HTML.File.2.MariaDB#

Screenshots:


[Image: 1-2022-02-07-13-27-20.png]
html link creator

[Image: 2-2022-02-07-13-28-37.png]

[Image: 3-2022-02-07-13-29-19.png]

A.2) Multiple URL's w/ Loops using a Linklist.txt > MariaDB using PyMySQL and BeautifulSoup4 (Multiple URL's = Multiple Rows Parsed) [Not Sure How to Modify A.1 to create a working A.2]

B.1) Local HTML File (From WGET Download) > MariaDB using PyMySQL and BeautifulSoup4 (Single HTML Local File = Single Row Parsed) [Solved: 02/07/2022 @ Approx. 20:23 ]

Source Tutorials/Blogs: https://gist.github.com/ScribbleGhost/e5...7fdb6ec9ea

Database Name: HolyBible
Table Name: Jeet-Kune-Do-B

Database Schematics as Follows:

Column #1: h1_text | Datatype: TEXT | Null
Column #2: p_text | Datatype: TEXT | Null
Working Python 3.9 Code is as Follows:

# 06_Jeet-Kune-Do.B.1
# 02/07/2022 
# Python 3.9.9
# Objective: Parse a Single HTML File using Beautiful Soup 4 and PyMySQL and Insert to MariaDB
# Target: kingsjamesbibleonline.org
# Securing God's Word in MariaDB for Sharpen Your Sword Ministries 
# Source 1: https://gist.github.com/ScribbleGhost/e5b6a808681004d207a3ee7fdb6ec9ea | Local HTML File Parsing
# python-forum.io Thread URL: https://python-forum.io/thread-36325-post-153148.html

import pymysql
import pymysql.cursors
from bs4 import BeautifulSoup


#soup = BeautifulSoup(open("index.html", encoding="utf8"), "html.parser")
soup = BeautifulSoup(open("Matthew-24-21/index.html", encoding="utf8"), "html.parser")

#print(soup.find_all("div", class_="someclass"))
#print(soup.find_all("h1").get_text())

print(soup.find("h1").get_text())
print(soup.find("p").get_text())

# Connection to MariaDB 10.5.x with a Database selected using PyMySQL
connection = pymysql.connect(host='localhost',
                 user='username',
                 password='password',
                 db='HolyBible',
                 charset='utf8mb4',
                 cursorclass=pymysql.cursors.DictCursor)

# Assign a Variable to BeautifulSoup4 Parsing using soup.find_all("") function 
# which is telling BeautifulSoup4 to find all <h1> & <p> tags
# and store tags signified as zero [0]  and then 
# strips the tags using soup.find_all("h1").get_text() & soup.find_all("p").get_text() 
# leaving us only the text to pass to MariaDB for storage

#h1_text = htmlParse.find_all("h1")[0].get_text()
#p_text = htmlParse.find_all("p")[0].get_text()

h1_text = soup.find("h1").get_text()
p_text = soup.find("p").get_text()

try: 
    with connection.cursor() as cursor: 
            sql = "INSERT INTO `Jeet-Kune-Do-B` (`h1_text`,`p_text`) VALUES (%s, %s)" 
            cursor.execute(sql, (h1_text,p_text)) 

    connection.commit() 
finally: 
    connection.close()

# Alert Brandon | Payload Delivered Successfully!
print ("Python Script Executed && Payload Delievered Successfully!")
Output after Running 06_Jeet-Kune-Do.B.1.py:

brandon@FireDragon:~/Desktop/Exodus.Python.Forum.io.Obstacles/06.Jeet-Kune-Do.B.1$ python3.9 06_Jeet-Kune-Do.B.1.py
Matthew 24:21
“For then shall be great tribulation, such as was not since the beginning of the world to this time, no, nor ever shall be.”
King James Version (KJV)
Python Script Executed && Payload Delievered Successfully!
brandon@FireDragon:~/Desktop/Exodus.Python.Forum.io.Obstacles/06.Jeet-Kune-Do.B.1$

Screenshots:


[Image: 1-2022-02-07-19-06-26.png]

[Image: 2-2022-02-07-19-07-14.png]

[Image: 3-2022-02-07-20-08-58.png]


B.2) Local HTML File(s) in a Folder (Batch from WGET Download) > MariaDB using PyMySQL and BeautifulSoup4 (Multiple HTML Local Files = Multiple Rows Parsed) [Not Sure How to Modify A.2 to create a working B.2]

Thank you everyone for this forum! I will edit this if/when I make progress on my own! Appreciate everyone!

Best Regards,

Brandon!
“And one of the elders saith unto me, Weep not: behold, the Lion of the tribe of Juda, the Root of David, hath prevailed to open the book,...” - Revelation 5:5 (KJV)

“And oppress not the widow, nor the fatherless, the stranger, nor the poor; and ...” - Zechariah 7:10 (KJV)

#LetHISPeopleGo

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Securing State Constitutions (USA) from University of Maryland > MariaDB .sql BrandonKastning 1 1,517 Jan-21-2022, 06:34 PM
Last Post: BrandonKastning
Exclamation Debian 10 Buster Environment - Python 3.x (MariaDB 10.4.21) | Working Connector? BrandonKastning 9 4,239 Jan-04-2022, 08:27 PM
Last Post: BrandonKastning
  Python Obstacles | Krav Maga | Wiki Scraped Content [Column Copy] BrandonKastning 4 2,219 Jan-03-2022, 06:59 AM
Last Post: BrandonKastning
  Python Obstacles | Kapap | Wiki Scraped Content [Column Nulling] BrandonKastning 2 1,726 Jan-03-2022, 04:26 AM
Last Post: BrandonKastning
Lightbulb Python Obstacles | Kung-Fu | Full File HTML Document Scrape and Store it in MariaDB BrandonKastning 5 2,880 Dec-29-2021, 02:26 AM
Last Post: BrandonKastning
  Python Obstacles | American Kenpo | Wiki Scrape URL/Table and Store it in MariaDB BrandonKastning 6 2,836 Dec-29-2021, 12:38 AM
Last Post: BrandonKastning
  Python Obstacles | Karate | HTML/Scrape Specific Tag and Store it in MariaDB BrandonKastning 8 3,162 Nov-22-2021, 01:38 AM
Last Post: BrandonKastning
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,619 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Any way to remove HTML tags from scraped data? (I want text only) SeBz2020uk 1 3,459 Nov-02-2020, 08:12 PM
Last Post: Larz60+
  Easy HTML Parser: Validating trs by attributes several tags deep? runswithascript 7 3,563 Aug-14-2020, 10:58 PM
Last Post: runswithascript

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020