1. 程式人生 > >python爬取json資料

python爬取json資料

# coding utf-8
import requests
import json
import datetime
import pymysql

user_agent = 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)'  # 用來偽裝使用者身份
headers = {'User-Agent': user_agent}
p2plist_url = 'http://www.wdzj.com/wdzj/html/json/dangan_search.json'  # 用來存放平臺的JSON資料頁面
print
('正在抓取……') s1 = datetime.datetime.now() r = requests.get(p2plist_url) content = r.text content = json.loads(content) # 載入JSON資料 s2 = datetime.datetime.now() s3 = (s2 - s1).total_seconds() print(content) print('抓取完成^\n共用時%s秒' % round(s3)) con = db = pymysql.connect(host="192.168.0.204", user="root", password="
jenkins!666", db="ceshi", port=3306) # 連線資料庫 cur = con.cursor() # 建立遊標 create_table = 'create table shuju (id smallint,name varchar(20),pin varchar(20))' # 建立一個數據庫用來儲存資料 try: cur.execute(create_table) con.commit() print('創表成功') except Exception as e: print('創表失敗\n%s ' % e) con.rollback()
# 插入資料語句 for x in content: sql = "insert into shuju (name,id,pin) values ('%s','%d','%s')" % (x['platName'], x['platId'], x['platPin']) try: cur.execute(sql) con.commit() print('插入成功 %s %s %s' % (x['platName'], x['platId'], x['platPin'])) except Exception as e: print('插入錯誤\n%s ' % e) con.rollback() cur.close() # 關閉遊標 con.close() # 關閉連線