Python csv文件的写入和读取

crv文件写入: # 引用csv模块。 import csv # 调用open()函数打开csv文件,传入参数:文件名“demo.csv”、写入模式“w”、newline=”、encoding=’utf-8’。 csv_file = open(‘demo.csv’,’w’,newline=”,encoding=’utf-8′) # 用csv.writer()函数创建一个writer对象。 writer = csv.writer(csv_file) # 调用writer对象的writerow()方法,可以在csv文件里写入一行文字 “电影”和“豆瓣评分”。 writer.writerow([‘电影’,’豆瓣评分’]) # 在csv文件里写入一行文字 “银河护卫队”和“8.0”。 writer.writerow([‘银河护卫队’,’8.0′]) # 在csv文件里写入一行文字 “复仇者联盟…

Read More

Python Excel文件新建写入读取

import openpyxl # 写入的代码: wb = openpyxl.Workbook() sheet = wb.active sheet.title = ‘new title’ sheet[‘A1’] = ‘漫威宇宙’ rows = [[‘美国队长’,’钢铁侠’,’蜘蛛侠’,’雷神’],[‘是’,’漫威’,’宇宙’, ‘经典’,’人物’]] for i in rows: sheet.append(i) print(rows) wb.save(‘Marvel.xlsx’) # 读取的代码: wb = openpyxl.load_workbook(‘Marvel.xlsx’) sheet = wb[‘new title’] sheetname = wb.sheetnames print(sheetname) A1_cell = sheet[‘A1’] A1_value = A1_ce…

Read More

Python 抓取QQ音乐列表案例

# 引用requests库 import requests # 调用get方法,下载这个字典 res_music = requests.get(‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=60997426243444153&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w=%E5%91%A8%E6%9D%B0%E4%BC%A6&g_tk=5381&loginUin=0&hostUin=0&forma…

Read More

Python+BeautifulSoup抓取电影天堂影片下载地址

实现这样的功能:用户输入喜欢的电影名字,程序即可在电影天堂https://www.ygdy8.com爬取电影所对应的下载链接,并将下载链接打印出来。 #调用quote()函数 from urllib.parse import quote # 调用requests库 import requests # 调用BeautifulSoup库 from bs4 import BeautifulSoup # 模拟页头信息 fakeHeaders = {‘User-Agent’: “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”} movie_name = input(‘请输入你需要搜索的电影名:’) # 将汉字,用gbk格式编码,赋值…

Read More

Python +BeautifulSoup 抓取豆瓣电影TOP250页面数据

#调用crv模块 最后把得到的数据写入crv import csv # 调用requests库 import requests # 调用BeautifulSoup库 from bs4 import BeautifulSoup # 模拟页头信息 fakeHeaders = {‘User-Agent’: “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)”} N = 10 #网站分页结构一样 代表扒10个页面 也就是扒到第10页 films2 = [] #空的数组来存储全部页数的数据 for i in range(1,N+1): start = (i-1)*25 #分页的页面变量 第1页是0 第2页25 第3页50…… #…

Read More

Python BeautifulSoup 抓取文章信息

# 调用requests库 import requests # 调用BeautifulSoup库 from bs4 import BeautifulSoup # 返回一个response对象,赋值给res res =requests.get(‘https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html’) # 把res解析为字符串 html=res.text # 把网页解析为BeautifulSoup对象 soup = BeautifulSoup( html,’html.parser’) # 通过匹配属性class=’books’提取出我们想要的元素 items = soup.find_all(class_=’books’) # 遍历列表items for item in item…

Read More

Python中print函数中中逗号和加号的区别

先看看print中逗号和加号分别打印出来的效果.. 这里以Python3为例 1 print(“hello” + “world”) helloworld 1 print(“hello”, “world”) hello world 这里发现加号的作用是连接字符串 而逗号相当于用空格连接字符串。 尝试一下不同数据类型的操作.. 1 print(“hello” + 123) TypeError: must be str, not int 1 print(“hello”, 123) hello 123 这里发现加号在Str类型与Num类型相加的出现了类型错误 逗号连接正常并返回字符串结果。 总结: 加号 +…

Read More

UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa9’ in position 3738: illegal multibyte sequence Python报错解决方案

# 调用requests模块 import requests # 获取网页源代码,得到的res是response对象。 res = requests.get(‘https://localprod.pandateacher.com/python-manuscript/crawler-html/spider-men5.0.html’) # 检测请求是否正确响应 print(res.status_code) # 正确响应,进行读写操作 # 新建一个名为book的html文档,你看到这里的文件没加路径,它会被保存在程序运行的当前目录下。 # 字符串需要以w读写。你在学习open()函数时接触过它。 if res.status_code == 200: file = open(‘book.html’,’w’) # res.text是字符串格式,把它写入文件内。 file.write(res.text…

Read More

Python 远程下载图片

import requests res = requests.get(‘https://res.pandateacher.com/2018-12-18-10-43-07.png’) #发出请求,并把返回的结果放在变量res中 pic=res.content #把Reponse对象的内容以二进制数据的形式返回 photo = open(‘ppt.jpg’,’wb’) #新建了一个文件ppt.jpg,这里的文件没加路径,它会被保存在程序运行的当前目录下。 #图片内容需要以二进制wb读写。你在学习open()函数时接触过它。 photo.write(pic) #获取pic的二进制内容 photo.close() #关闭文件

Python 抽奖器代码

# 查看注释,运行代码。 import random import time # 将抽奖程序封装成函数 def choujiang(*ac): # 定义一个抽奖函数,不定长参数,也就是不限制人数个数 luckylist = [*ac] # 定义一个中奖名单的列表 a = random.choice(luckylist) # 在中奖名单里面随机选择 print(‘开奖倒计时’,3) time.sleep(1) print(‘开奖倒计时’,2) time.sleep(1) print(‘开奖倒计时’,1) time.sleep(1) image = ”’ /\_)o< | \\ | O . O| \_____/ ”’ print(image) print(‘恭喜’+a+’中奖!’) choujiang(‘虚竹’,’萧峰’,’段誉’,’紫嫣’,’段王爷’) # 调用函数