将mysql的表导出成csv。
在这里插入图片描述
import os
import uuid
from uuid import UUID
import json
filename='test'
if not os.path.exists(filename):
os.mkdir(filename)
else:
print('已有')
print(os.getcwd())#得到绝对路径
print(os.path.join(os.getcwd(),'test'))
print(uuid.uuid1())
print(uuid.uuid3(UUID(int=1),"no"))
text="hellow world"
with open('test.text','a',encoding='utf8') as f:
f.write(text)
with open('test.json','r',encoding='utf8')as f:
print(json.load(f))
res=f.read()
print(res)
# data='{'name':'python'}' json格式不能是单引号
data='{"name":"python"}'
# with open('test.json ','a',encoding='utf8') as f:
# json.dump(data,f)
res=json.loads(data)
print(res)
作业(爬取qq新闻)
from requests import get
import os
import json
import csv
url='https://i.news.qq.com/trpc.qqnews_web.kv_srv.kv_srv_http_proxy/list?sub_srv_id=24hours&srv_id=pc&offset=40&limit=20&strategy=1&ext={%22pool%22:[%22top%22],%22is_filter%22:7,%22check_type%22:true}'
headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36 Edg/85.0.564.51',
'referer': 'https://news.qq.com/'}
def get_html(url):
html=get(url,headers=headers)
if html.status_code==200:
res=html.json()['data']['list']
for r in res:
print(r['title']+'\n'+r['url'])
storage_csv((r['title'],r['url']))
# storage_text(r['title']+'\n'+r['url']+'\n')
else:
print('error')
def storage_text(test):
with open('qqNEWS/qqnews.text', 'a+', encoding='utf8') as f:
f.write(test)
def storage_csv(test1):
with open('qqNEWS/qqnews.csv','a',encoding='utf8')as f:
csv_f=csv.writer(f)
csv_f.writerow(test1)
if __name__=="__main__":
filename='qqNEWS'
if not os.path.exists(filename):
os.makedirs(filename)
get_html(url)
学习csv的4个api
import csv
def get_csv():
with open('test.csv',encoding='utf8')as f:
f_csv=csv.reader(f)
next(f_csv)
for c in f_csv:
print(c)
def write_csv(test):
with open('test.csv','a+') as f:
f_csv=csv.writer(f)
f_csv.writerow(('name','age'))#操作单行
f_csv.writerows(test)
if __name__=="__main__":
test=[('name','age'),
('name','age'),
('name','age')]
# get_csv()
write_csv(test)