pythonSpider_requests获取豆瓣音乐信息写入csv

简介: pythonSpider_requests获取豆瓣音乐信息写入csv

查看网页

歌名

表演者

特点

都是在<span class="pl">里面,可以使用正则表达式提取出来

正则提取

对html的节点进行提取

#正则提取风格、时间、出版人
findStyle=re.compile(r'<span class="pl">流派:</span>&nbsp;(.*?)<br />',re.S)# re.S忽略换行
findTime=re.compile(r'<span class="pl">发行时间:</span>&nbsp;(.*?)<br />',re.S)
findPublish=re.compile(r'<span class="pl">出版者:</span>&nbsp;(.*?)<br />',re.S)

实现

import requests,time,urllib.request,urllib
from  bs4 import BeautifulSoup #需要安装xlml库
import  csv,re
#正则提取风格、时间、出版人
findStyle=re.compile(r'<span class="pl">流派:</span>&nbsp;(.*?)<br />',re.S)# re.S忽略换行
findTime=re.compile(r'<span class="pl">发行时间:</span>&nbsp;(.*?)<br />',re.S)
findPublish=re.compile(r'<span class="pl">出版者:</span>&nbsp;(.*?)<br />',re.S)
headers={
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
}
def get_url_music(filename,url):#访问url,提取出链接
    html=requests.get(url,headers=headers)
    soup=BeautifulSoup(html.text,'lxml') #
    aTags=soup.find_all('a',attrs={'class':'nbg'})#链接
    for aTag in aTags:
        new=aTag['href']
        get_music_info(filename,new)#调用
    return True
def get_music_info(filename,url):#进入链接
    html=requests.get(url,headers=headers)
    htmlText=html.text
    print(htmlText)
    soup=BeautifulSoup(htmlText,'lxml') #
    name=soup.find(attrs={'id':'wrapper'}).h1.span.text#歌名
    author=soup.find(attrs={'id':'info'}).find('a').text#发行者
    styles=re.findall(findStyle,htmlText)[0]#风格
    styles=re.sub('<br(\s+)?/>(\s+)?','',styles)
    styles = re.sub('/', ' ', styles)
    styles = "".join(styles.split())#前后空格
    # styles = re.findall('<span class="pl">流派:</span>&nbsp;(.*?)<br/>',htmlText)  # 风格
    style = '未知' if len(styles)==0 else styles#处理前后空格
    time=re.findall(findTime,htmlText)[0]#时间
    time="".join(time.split())
    publishers=re.findall(findPublish,htmlText)
    publisher='未知' if len(publishers)==0 else publishers[0].strip()
    info={
        'name':name,
        'author':author,
        'style':style,
        'time':time,
        'publisher':publisher
    }#结果字典数据
    print(info)
    save_csv(filename,info)#保存数据
    return info
def save_csv(filename,info):#写入每一列的数据
    with open(filename,'a',encoding='utf-8') as f:
        fieldnames=['name','author','style','time','publisher']
        writer=csv.DictWriter(f,fieldnames=fieldnames)#列数据
        writer.writerow(info)
if __name__=='__main__':
    urls=['https://music.douban.com/top250?start={}'.format(str(i)) for i in range(0,1,25)]
    print(urls)
    filename='douban_music.csv'
    with open(filename,'w',encoding='utf-8')as f:
        fieldnames=['name','author','style','time','publisher']
        writer=csv.DictWriter(f,fieldnames=fieldnames) # 表头
        writer.writeheader()
    for url in urls:
        get_url_music(filename,url)#调用
        time.sleep(1)#延迟1

结果

处理换行

添加newlinew

加上newline=''

def save_csv(filename,info):#写入每一列的数据
    with open(filename,'a',encoding='utf-8',newline='') as f:# newline解决换行
        fieldnames=['name','author','style','time','publisher']
        writer=csv.DictWriter(f,fieldnames=fieldnames)#列数据
        writer.writerow(info)

代码:我的仓库

目录
相关文章
|
9月前
|
Python
pythonSpider_urllib获取豆瓣电影top250信息写入excel
pythonSpider_urllib获取豆瓣电影top250信息写入excel
73 0
|
数据采集 Python
基于bs4+requests的豆瓣电影爬虫
1.爬取豆瓣电影前250详情页面 豆瓣电影前250详情页面持久化为250个htm文件,打包文件下载链接: https://pan.baidu.com/s/1_zlZJQJtl9pPEJUGYVMYaw 密码: ehrq 文件解压后的文件夹命名为doubanSourcePages,下面代码复制到py文件中,py文件和doubanSourcePages文件夹在同一级目录下。
1348 0
|
Web App开发 Python Windows
python爬取猫眼电影 top 100 保存到CSV
代码没含量,希望帮到入门的小白。 import requests import re,json from lxml import etree import csv class Spider(): def open_csv(self): ...
1808 0
|
JSON 数据格式 Python
Crawler:基于requests库+json库+40行代码实现爬取猫眼榜单TOP100榜电影名称主要信息
Crawler:基于requests库+json库+40行代码实现爬取猫眼榜单TOP100榜电影名称主要信息
Crawler:基于requests库+json库+40行代码实现爬取猫眼榜单TOP100榜电影名称主要信息
|
数据采集
【详细步骤解析】爬虫小练习——爬取豆瓣Top250电影,最后以csv文件保存,附源码
【详细步骤解析】爬虫小练习——爬取豆瓣Top250电影,最后以csv文件保存,附源码
334 0
|
Web App开发 JSON 数据格式
Requests库+正则爬取猫眼电影Top100
声明:此篇文章主要是观看静觅教学视频后做的笔记,原教程地址:https://cuiqingcai.com/ 流程框架 1.抓取单页内容:利用requests请求目标站点,得到单个网页HTML代码,返回结果。
1294 0
|
数据采集 Java Python
Python爬虫之多线程下载豆瓣Top250电影图片
爬虫项目介绍   本次爬虫项目将爬取豆瓣Top250电影的图片,其网址为:https://movie.douban.com/top250, 具体页面如下图所示:   本次爬虫项目将分别不使用多线程和使用多线程来完成,通过两者的对比,显示出多线程在爬虫项目中的巨大优势。
2476 0
|
文件存储 Python
简单爬取豆瓣电影相关信息
简单爬取豆瓣电影相关信息
187 0
简单爬取豆瓣电影相关信息
|
Web App开发 iOS开发 Python
|
Python 数据采集
用Python爬取网易云音乐歌曲
前天给大家分享了用Python网络爬虫爬取了网易云歌词,在文尾说要爬取网易云歌曲,今天小编带大家一起来利用Python爬取网易云音乐,分分钟将网站上的音乐down到本地。
2176 1

热门文章

最新文章