import json
import requests
import pymysql
# mysql_coon 主要的功能就是, 將鏈結資料庫的操作變成只連線一次
#class
mysql_conn
(object):
# 魔術方法, 初始化, 建構函式
def__init__
(self):
self.db = pymysql.connect(host='127.0.0.1', user='root', password='123456', port=3306, database='py1011')
self.cursor = self.db.cursor()
# 執行modify(修改)相關的操作
defexecute_modify_mysql
(self, sql):
self.cursor.execute(sql)
self.db.commit()
# 魔術方法, 析構化 ,析構函式
def__del__
(self):
self.cursor.close()
self.db.close()
# 因為不能訪問, 所以我們加個頭試試
headers =
# urllib 的相關操作如下
url = ''
# #
# #
# #
# #
# response = requests.get(url, headers=headers)
res_dict = json.loads(response.text)
list_list = res_dict['list']
#print(list_list)
# 遍歷 list_list
data = {}
for list_item_dict in list_list:
# list 列表內的乙個item, 他是乙個dict
data_str = list_item_dict['data']
data_dict = json.loads(data_str)
data['ids'] = data_dict['id']
data['title'] = data_dict['title']
data['description'] = data_dict['description']
data['target'] = data_dict['target']
print(data_dict['id'])
print(data_dict['title'])
print(data_dict['description'])
print(data_dict['target'])
# print(list_item_dict)
try:
sql = 'insert into xueqiu(ids,title,description,target) values("","","","")'.format(**data)
mc = mysql_conn()
mc.execute_modify_mysql(sql)
print('以上內容爬取成功')
print('-' * 50)
except:
print('以上內容出錯,沒有存到資料庫')
print('-' * 50)
爬取雪球網案例
import pymysql class mysql conn object 魔術方法,初始化,析構函式 def init self self.db pymysql.connect localhost root xueqiu text 建立游標物件 self.cursor self.db.curso...
雪球網爬取資料並存入資料庫
from urllib import request import json import pymysql class mysql connect object 初始化的建構函式 def init self self.db pymysql.connect host 127.0.0.1 user ro...
爬蟲day3 (爬取雪球網n頁資料)
爬取雪球網n頁資料 用到 與mysql資料庫的互動 import requests import json import pymysql class mysql conn object 魔術方法,初始化,建構函式 def init self self.db pymysql.connect host ...