Day3笔记

  • requests包的使用
    import requests
    proxy = {'http':'http://(auth)ip:port'}
    url = 'target_webaddress'
    headers = {
    copy from target_web
    }
    response = requests.get(url,headers=headers,)
    print(response.text)
  • json类型处理
import json
from urllib import request
headers = {
    #'Accept': '*/*',
    #'Accept-Encoding': 'gzip, deflate, br',
    #'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    #'Connection': 'keep-alive',
    'Cookie': 'aliyungf_tc=AQAAALoQF3p02gsAUhVFebQ3uBBNZn+H; xq_a_token=584d0cf8d5a5a9809761f2244d8d272bac729ed4; xq_a_token.sig=x0gT9jm6qnwd-ddLu66T3A8KiVA; xq_r_token=98f278457fc4e1e5eb0846e36a7296e642b8138a; xq_r_token.sig=2Uxv_DgYTcCjz7qx4j570JpNHIs; _ga=GA1.2.516718356.1534295265; _gid=GA1.2.1050085592.1534295265; u=301534295266356; device_id=f5c21e143ce8060c74a2de7cbcddf0b8; Hm_lvt_1db88642e346389874251b5a1eded6e3=1534295265,1534295722; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1534295722',
    #'Host': 'xueqiu.com',
    #'Referer': 'https://xueqiu.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
    #'X-Requested-With': 'XMLHttpRequest',
    #'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}
url = 'https://xueqiu.com/v4/statuses/public_timeline_by_category.json?since_id=-1&max_id=-1&count=10&category=111'
req = request.Request(url, headers=headers)
response = request.urlopen(req)
res = response.read()
# 转化函数 res_dict = json.loads(res)
res_dict = json.loads(res.decode('utf-8'))
list_list = res_dict['list']
#print(list_list)
# 遍历 list_list
for list_item_dict in list_list:
    # list 列表内的一个item, 他是一个dict
    data_str = list_item_dict['data']
    print(data_str)
    print('-'*50)
  • 数据传输测试
import requests
import json


payload = {'key1': ('value1', 'value2')}
r = requests.post('http://httpbin.org/post', data=json.dumps(payload))

print(r.text)
  • 将数据库的连接操作封装为类
import pymysql

class Mysql_connect(object):
    def __init__(self):
    # 只创建一次连接对象
        self.db = pymysql.connect(host='127.0.0.1',user='root',password='123456',port=3306,database='db815',charset='utf8')
        self.cursor = self.db.cursor()

    def execute_sql(self,sql):
        self.cursor.execute(sql)
        self.db.commit()

    def __del__(self):
    # 关闭连接
        self.cursor.close()
        self.db.close()
  • 正则
import re
# 常用方法
# re.match(),re.search(),re.compile()
# 正则解释,正则即一种规则,或者说一种条件,主要用来寻找符合设定
# 规则的字符串。
target_str='********************'
re.match('abc',target_str) 
# re.match()默认从目标字符串的起始位置开始匹配,如匹配a开头字符串:
re.match('a',target_str)
# 一个字符用'.'表示
# 任意数量字符用'*'表示,'*'对左边邻近的一个字符生效
# 以某个字符结束在该字符后加'$'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值