Here is the code of my parser
parser v2
Import Requests.
From BS4 Import Beautifulsoup
Import pymysql.cursors.
DEF Get_HTML (URL):
R = Requests.get (URL)
Return R.Text
DEF Get_Data (HTML):
Soup = Beautifulsoup (HTML, 'LXML')
title = Soup.find ('H1'). Text
Date = Soup.find ('Span', {'Class': 'NEWS_DATE'}). Text
Text = Soup.find ('p'). Text
Textp = Soup.find_all ('p')
Print (Title)
PRINT (DATE)
PRINT (text)
Print (STR (TextP))
DEF Main ():
url = 'https://www.zakon.kz/4979616-kazahstan-spustya-12-let-vyigral-'
(Get_Data (Get_HTML (URL)))
if __name__ == '__main__':
Main ()
and I need the data from the parser automatically recorded in the database which Pymysql
Here is the code of my connection to pymysql
connection = pymysql.connect (host = 'localhost',
user = 'root',
password = '',
db = 'Pars',
Charset = 'UTF8',
Autocommit = True)
Print ("Connect Successful !!")
How to implement data from parsing automatically did insert
Answer 1, Authority 100%
If I understood everything correctly. Table in the database ‘MYTABLE’
Def Add (Title, Date, Text, Textp):
Connection = Pymysql.connect (host = 'localhost', user = 'root', password = '', db = 'pars', charset = 'utf8mb4', cursorclass = pymysql.cursors.dictcursor)
Try:
With Connection.cursor () As Cursor:
SQL = "INSERT INTO MYTABLE (` title`, `date`,` text`, `Textp`) Values (% s,% s,% s,% s)
Cursor.Execute (SQL, (Title, Date, Text, Textp))
Connection.comMit ()
Finally:
Connection.Close ()
Answer 2
Found a way to implement
connection = pymysql.connect (host = 'localhost',
user = 'root',
password = '',
db = 'Pars',
Charset = 'UTF8',
Autocommit = True)
Print ("Connect Successful !!")
url = 'https://www.zakon.kz/4979527-shestnadtsatiletniy-kibersportsmen.html'
Array = Get_Data (Get_HTML (URL))
SQL = "INSERT INTO GAG (` Title`, `text`,` time`, `textp`) Values (% S,% S,% S,% s)"
Cursor = Connection.cursor ()
title = array [0]
Text = Array [1]
Date = STR (Array [2])
TextP = Array [3]
Cursor.Execute (SQL, (STR (TITLE), STR (TEXT), STR (DATE), STR (TEXTP)))
Print ("Connect Successful!")