案例:
01壁纸链接爬取并保存csv
#导入模块
import requests
import chardet
from bs4 import BeautifulSoup
#访问链接
url = 'http://www.netbian.com/dongman/'
#打开链接,得到响应
res = requests.get(url)
#获取编码
chardet.detect(res.content)
#设置网页编码
res.encoding = 'GB2312'
# 转为 soup 对象
soup = BeautifulSoup(res.text ,'html.parser')
imgss=soup.find(class_='list').find_all('img')
srcs = []
for each in imgss:
srcs.append(each.attrs['src'])
srcs
# 存储二维列表专用类
class MySave():
def __init__(self):
pass
def saveToCsv(self,data:list, fileName:str, mode='w'):
with open(fileName,mode=mode,encoding='utf-8',newline='') as f:
csvfile = csv.writer(f)
# 写入data
for each in data:
csvfile.writerow(each)
print(fileName,'存储完成。')
list2 = [[each] for each in srcs]
list2
save =MySave()
import csv
save.saveToCsv(list2,'彼岸壁纸.csv','w')