爬虫(二)BeautifulSoup,解析数据,提取数据,find,find_all,select用法,爬取豆瓣250排行榜,下电影

本文介绍了如何使用BeautifulSoup库解析和提取网页数据,详细讲解了find(), find_all()以及select()方法,并通过实例展示了如何爬取豆瓣Top250电影列表并下载电影信息。" 84770680,1317631,JFinal Controller中处理JSON数据,"['Java', 'Web开发', 'JFinal框架', 'Ajax', 'JSON处理']
摘要由CSDN通过智能技术生成

BeautifulSoup

解析数据

提取数据

find()
find_all()
tag对象
select用法

实例

爬取豆瓣250
下电影

BeautifulSoup

使用BeautifulSoup 解析和提取网页中的数据
安装库 pip install BeautifulSoup4

解析数据

解析数据的方法是用BeautifulSoup()
在这里插入图片描述

import requests     
from bs4 import BeautifulSoup    #引入BS库

res=requests.get('http://www.zongheng.com/rank.html')
sp=BeautifulSoup(res.text,'html.parser')     #括号中的第0个参数,必须是字符串类型;第1个参数是解析器

print(type(sp))
><class 'bs4.BeautifulSoup'>  

提取数据

利用bs解析数据后就可以利用bs中的方法来提取数据。
方法一: find()与find_all()
find()只提取首个满足要求的数据,而find_all()提取出的是所有满足要求的数据。
在这里插入图片描述

import requests     
from bs4 import BeautifulSoup    #引入BS库

res=requests.get('http://www.zongheng.com/rank.html')
sp=BeautifulSoup(res.text,'html.parser')     #用BS解析数据,括号中的第0个参数,必须是字符串类型;第1个参数是解析器

tp=sp.find_all('div',class_="borderB_c_dsh")
print(type(tp))

><class 'bs4.element.ResultSet'>   #相当于Tag对象以列表结构储存了起来,可以把它当做列表来处理

Tag对象

import requests     
from bs4 import BeautifulSoup    #引入BS库

res=requests.get('http://www.zongheng.com/rank.html')
sp=BeautifulSoup(res.text,'html.parser')     #括号中的第0个参数,必须是字符串类型;第1个参数是解析器

tp=
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是完整代码: ```python import requests from bs4 import BeautifulSoup import pymysql from wordcloud import WordCloud import matplotlib.pyplot as plt # 连接MySQL数据库 db = pymysql.connect(host='localhost', user='root', password='password', port=3306, db='douban_music') cursor = db.cursor() # 创建表格 sql = 'CREATE TABLE IF NOT EXISTS music (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), artist VARCHAR(255), rating FLOAT, num_of_comments INT)' cursor.execute(sql) # 爬取音乐排行榜 url = 'https://music.douban.com/top250' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') music_list = soup.find_all('tr', class_='item') # 将数据存入MySQL数据库 for music in music_list: name = music.find_all('a')[1].text.strip() artist = music.find_all('p')[0].text.strip().split('/')[0] rating = float(music.find_all('span', class_='rating_num')[0].text.strip()) num_of_comments = int(music.find_all('span', class_='pl')[0].text.strip().split()[1][:-3]) sql = 'INSERT INTO music(name, artist, rating, num_of_comments) values(%s, %s, %s, %s)' cursor.execute(sql, (name, artist, rating, num_of_comments)) db.commit() # 从MySQL数据库中读取数据并生成词云图 sql = 'SELECT name FROM music' cursor.execute(sql) results = cursor.fetchall() text = '' for result in results: text += result[0] + ' ' wordcloud = WordCloud(background_color='white', width=800, height=600, margin=2, font_path='msyh.ttc').generate(text) plt.imshow(wordcloud) plt.axis('off') plt.show() # 关闭数据库连接 cursor.close() db.close() ``` 这段代码首先连接MySQL数据库并创建一个名为“music”的表格,然后爬取豆瓣音乐Top250页面上的音乐信息,并将这些信息存入MySQL数据库中。最后,从数据库中读取音乐名称,将它们拼接成一个字符串,并生成一个词云图,用于展示所有音乐的名称。 请确保在运行代码之前已经安装了以下库: - requests - bs4 - pymysql - wordcloud - matplotlib 在生成词云图之前,需要先下载并安装微软雅黑字体(msyh.ttc),可以在以下网址下载:https://www.fontpalace.com/font-download/Microsoft+YaHei/ 在运行代码之前,需要先修改数据库连接信息,将用户名、密码、端口号和数据库名称改为您自己的信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值