Python+selenium +BeautifulSoup抓取QQ音乐精彩评论(涉及点击加载更多的处理方式)

# 本地Chrome浏览器设置方法 from selenium import webdriver import time #本地一定得在环境根目录下安装谷歌浏览器的驱动 driver = webdriver.Chrome() from bs4 import BeautifulSoup import time driver.get(‘https://y.qq.com/n/yqq/song/000xdZuV2LcQ19.html’) # 访问页面 time.sleep(2) #点击3次页面的‘点击加载更多’按钮 加上默认的15条数据,最后就有60条热评了 for i in range(3): button = driver.find_element_by_class_name(‘js_get_more_hot’) # 根据类名找到【点击加载更多】 button.click() # 点击 tim…

Read More

Python selenium提取数据的方法

# 以下方法都可以从网页中提取出’你好,蜘蛛侠!’这段文字 find_element_by_tag_name:通过元素的名称选择 # 如<h1>你好,蜘蛛侠!</h1> # 可以使用find_element_by_tag_name(‘h1’) find_element_by_class_name:通过元素的class属性选择 # 如<h1 class=”title”>你好,蜘蛛侠!</h1> # 可以使用find_element_by_class_name(‘title’) find_element_by_id:通过元素的id选择 # 如<h1 id=”title”>你好,蜘蛛侠!</h1> # 可以使用find_element_by_id(‘title’) find_element_by_name:通过元素的name…

Read More

Python 获取QQ音乐信息并且保存成xlsx文件

import requests,openpyxl # 创建工作薄 wb=openpyxl.Workbook() # 获取工作薄的活动表 sheet=wb.active # 工作表重命名 sheet.title=’lyrics’ sheet[‘A1′] =’歌曲名’ # 加表头,给A1单元格赋值 sheet[‘B1′] =’所属专辑’ # 加表头,给B1单元格赋值 sheet[‘C1′] =’播放时长’ # 加表头,给C1单元格赋值 sheet[‘D1′] =’播放链接’ # 加表头,给D1单元格赋值 url = ‘https://c.y.qq.com/soso/fcgi-bin/client_search_cp’ for x in range(5): params = { ‘ct’: ’24’, ‘qqmusic_ver’: ‘1298’, ‘new_json’: ‘1’, ‘remote…

Read More

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