Skip to content

Commit

Permalink
work2.test02
Browse files Browse the repository at this point in the history
add test02
  • Loading branch information
lyc280705 committed Oct 13, 2023
1 parent b945647 commit 4585695
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions work2/lyc280705/02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import requests
import re
import pymysql
basic_url="https://baike.baidu.com/cms/home/eventsOnHistory/"
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31'
}
db = pymysql.connect(host='localhost', port=3306, user='root', password='lyc050728', database='history')
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS history;")
create_table_sql = """
CREATE TABLE history (
id INT(11) NOT NULL AUTO_INCREMENT,
year VARCHAR(50),
title_text VARCHAR(255),
_type VARCHAR(255),
desc_text TEXT,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
"""
cursor.execute(create_table_sql)
for i in range(1,13):
month_str='{:02d}'.format(i)
url=basic_url+month_str+".json?_=16970370778"+str(95-i)
response = requests.get(url=url, headers=headers)
data=response.json()
for month in data.values():
for day in month.values():
for event in day:
if 'year' in event:
desc = event['desc']
title = event['title']
year = event['year']
_type=event['type']
print(event['year'],event['type'])
desc_text = re.sub('<.*?>', '', desc)
title_text = re.sub('<.*?>', '', title)
print(title_text,desc_text)
sql1 ="INSERT INTO history (year,_type, title_text, desc_text) VALUES (%s, %s, %s,%s)"
cursor.execute(sql1,(str(year), str(_type), str(title_text), str(desc_text)))
db.commit()
db.close()

0 comments on commit 4585695

Please sign in to comment.