# CODING=UTF-8 Import JSON import requests from BS4 import beautifulsoup Import db # Get Data def get_data (URL): hea DERs = {' user-agent ': ' mozilla/5.0 (Windows NT 10.0;
WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/63.0.3239.84 safari/537.36 '};
Response = Requests.post (URL, headers=headers, verify=false);
response.encoding = ' utf-8 ';
Text = Response.text;
# text = Text.replace (', ');
# text = Text.replace (', ');
Text = Text.replace (' \ n ', ');
Text = Text.replace (' \ R ', ');
return text;
# Data Write to File Def writedata (listdata): Db.dbinsert (listdata) # file = Open ("Jd.txt", "A +", encoding= ' utf-8 ');
# for data in Listdata: # WriteData = json.dumps (data, ensure_ascii=false);
# file.writelines (WriteData);
# file.writelines (' \ n ');
# File.flush ();
# file.close ();
# Analysis of the desired data # Number of data formats returned list<object> def analyzedata (html): Listdata = [];
Soup = beautifulsoup (html, ' lxml '); ContentList = Soup.select (' Div.p-name ');
For content in contentlist:detail = {};
detail[' key '] = Content.select (' a ') [0].attrs.get (' href ');
detail[' value '] = Content.text.strip ();
Listdata.append (detail);
return listdata;
page = 0;
Lastkey = ';
While True: # paging Fetch Data page = page + 1;
url = "";
data = Get_data (URL);
Listdata = analyzedata (data);
Lastkeytemp = Listdata[len (listdata)-1].get (' key ');
if lastkeytemp = = Lastkey:break;
Else:lastkey = lastkeytemp;
WriteData (Listdata);
Import PSYCOPG2
def dbinsert (listdata):
conn = Psycopg2.connect (database= ", user=", password=
',
port= ' 5432 ')
cur = conn.cursor () for
data in Listdata:
try:
Cur.execute (r "" INSERT into T_ Data ("Key", "value") VALUES (' "" "+ data[' key '] +" "" "," "" "+ data[
' value '] +" "" "" "")
except PSYCOPG2. Error:
continue
Conn.commit ()
# Print (' Insert successfully ')
Conn.close ()