Home > other >  Python can't submit to the MySQL data?
Python can't submit to the MySQL data?

Time:09-22

The import requests
The import re
The import pymysql

Headers={' the user-agent ':' Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 '}

Def baidu (company) :
Url="https://www.baidu.com/s? RTT=1 & amp; BSST=1 & amp; Cl=2 & amp; Tn=news& The word='+ company
Res=requests. Get (url, headers=headers). The text
P_href='https://bbs.csdn.net/topics/

. *? P_title='& lt; H3 & gt; . *?> (. *?) '
P_info='& lt; P & gt; (. *?)

'
Href=https://bbs.csdn.net/topics/re.findall (p_href, res, re S)
Title=re. The.findall (p_title, res, re S)
Info=re. The.findall (p_info, res, re S)

# data cleaning and print
Source=[]
The date=[]

For I in range (len (title) :
Title=title [I] [I] strip ()
Title=[I] re. Sub (' & lt; . *?> ', ' ', the title [I])
Info. [I]=re sub (' & lt; . *?> ', ' ', the info [I])
Source. Append (info [I]. Split (' & amp; nbsp; & nbsp; ') [0])
Date. Append (info [I]. Split (' & amp; nbsp; & nbsp; ') [1])
The source [I]=source [I] strip ()
The date [I]=date [I] strip ()
# print (date [I])

For I in range (len (title) :
Db=pymysql. Connect (host='localhost', the port=3306, user='root' and password=', the database='pachong' charset='utf8')
Cur=the cursor (#) for session a pointer, used to invoke the SQL statement
SQL="INSERT INTO test (href company, title, date, source) VALUES (% s, % s, % s, % s, % s) '# write SQL statement
Cur. Execute (SQL, (the company, the title [I], href [I], the date [I], source [I]) # executing SQL statements
MIT ()
db.comSession # close cur. Close () pointer
The close () # close the database connection
Baidu (' alibaba ')

Prompt invalid syntax position in the db.com MIT ()

CodePudding user response:

False statements above a row, the last one less parentheses, careful...

CodePudding user response:

Thank you, later will note, had just begun to learn, many are still don't see don't understand

  • Related