雪球--数据的爬取并存入数据库

往数据库添加数据的一般步骤

#<1>导包

import pymysql

def add_pymysql(house_id,target,description):

#<2>创建连接 内部参数为ip 用户 密码 端口号(默认) 数据库 编码格式为’utf8’

db=pymysql.connect(host=’127.0.0.1’,user=’root’,password=’123456’,database=’pachong’,charset=’utf8’)

#<3>创建游标对象

cursor=db.cursor()

#<4>sql 数据添加到数据库的查询语句

# sql=”insert into xueqiu values(null,{},{},{})”.format(house_id,str(target),str(description))

sql = “insert into xueqiu values(null,’%s’,’%s’,’%s’)” % (house_id,target,description)

#<5>执行添加的过程

cursor.execute(sql)

#<6>执行添加提交

db.commit()

# <7)关闭游标

cursor.close()

db.close()

往数据库添加数据的一般步骤

<1>导包

import pymysql
class class_tt:
def init(self):
#<2>创建连接 内部参数为ip 用户 密码 端口号(默认) 数据库 编码格式为’utf8’
self.db=pymysql.connect(host=’127.0.0.1’,user=’root’,password=’123456’,database=’pachong’,charset=’utf8’)
#<3>创建游标对象
self.cursor=self.db.cursor()
def add_pymysql(self,sql):
#<4>执行添加的过程
self.cursor.execute(sql)
#<5>执行添加提交
self.db.commit()
# <6)析构函数 执行完毕自动关闭
def del(self):
self.cursor.close()
self.db.close()

实例化对象

s = class_tt()

爬取大量的数据的步骤(以雪球网——房产这栏为例)

《1》要分析怎样才能通过程序自动生成路由

《2》找到的规律是 变量有两个 count=10(第一页) max_id=-1 (第一页)

《3》第二个规律从第二页开始 count都是15 max_id都是上一页数据走后一条的next_max_id

《4》根据规律动态生成url

导入包

from urllib import request
import requests
import json

定义函数 house 用于爬取的雪球网的网页 参数为爬取的页码数

def house(pages):
# 由于没有的路由都不相同所以需要在找出规律之后动态生成路由
max_id=-1
for page in range(1,pages+1):
# 如果第一页 路由是固定的
if page==1:
url=’https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111
# #如果不是第一页 那么根据动态获取max_id 来动态生成url
else:
url=’https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id={}&count=15&category=111’.format(max_id)
headers={
‘Cookie’:’aliyungf_tc=AQAAALPHHxIycwEAvQCIdffypG6UciKV; xq_a_token=584d0cf8d5a5a9809761f2244d8d272bac729ed4; xq_a_token.sig=x0gT9jm6qnwd-ddLu66T3A8KiVA; xq_r_token=98f278457fc4e1e5eb0846e36a7296e642b8138a; xq_r_token.sig=2Uxv_DgYTcCjz7qx4j570JpNHIs; u=571534594712698; device_id=33d1e8b88c47f086de994a2eb451d350; _ga=GA1.2.1111839251.1534594714; _gid=GA1.2.784161144.1534594714; Hm_lvt_1db88642e346389874251b5a1eded6e3=1534594714,1534594964; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1534594964’,
‘User-Agent’:’Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36’,
}
#获取请求道德内容
response=requests.get(url,headers=headers)
#转化为字符串
html_text=response.text
#转化为字典
html_dict=json.loads(html_text)
#打印字典的内容
# print(html_dict)
#获取本页的next_max_id 用于生成下一页的url
max_id=html_dict[‘next_max_id’]
# #如果为第一页 有10条数据 如果不是有十五条数据
if page ==1:
count=10
else:
count=15
#获取每一页中我们想要的数据
for i in range(0,count):
#定位到我们想要的数据 切数据为json格式
str=html_dict[‘list’][i][‘data’]
#把json 格式的数据转化为 字典格式
html_dict_in=json.loads(str)
#获取id target description
house_id=html_dict_in[‘id’]
target=html_dict_in[‘target’]
description=html_dict_in[“description”]
#(1)类的实例化(2)书写sql语句(3)调用实例化对象中函数执行添加数据到数据库
sql = “insert into xueqiu values(null,’%s’,’%s’,’%s’)” % (house_id, target, description)
s.add_pymysql(sql)
#打印需要的结果
if name==’main‘:
#打印两页的内容
house(2)

猜你喜欢

转载自blog.csdn.net/chengjintao1121/article/details/81814323