- 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)
# 一个字符用'.'表示
# 任意数量字符用'*'表示,'*'对左边邻近的一个字符生效
# 以某个字符结束在该字符后加'$'