python爬取电影网站存储于数据库_(转)Python3爬取豆瓣电影保存到MySQL数据库

48行代码实现Python3爬取豆瓣电影排行榜

代码基于python3,用到的类库有:

标题文字

requests:通过伪造请求头或设置代理等方式获取页面内容,参考文档

BeautifulSoup:对页面进行解析,提取数据,参考文档

PyMySQL:python3版本中用于操作MySQL数据库,python2中则使用mysqldb,Github

pip安装用到的几个类库:

pip install requests

pip install bs4

pip install pymysql

分析豆瓣电影页面

页面分析:

爬取数据之前,我们都需要对页面进行分析,看我们可以从中提取到哪些数据,从下图我们看到豆瓣电影top250的页面结构,我们可以从中提取出排行榜(rank)、电影名字(name)、电影详情页链接(link)、电影海报(poster)、电影评分(score)、电影评论(quote)等,我在图中进行了标注

URL分析:

通过点击分页我们可以发现URL的格式为:https://movie.douban.com/top2...

其中num表示25的倍数的数字,最小是0也就是第一页,最大为225也就是最后一页,这可以作为我们爬取页面的限制条件,filter为过滤条件这里可不用管

代码

引入类库:

import pymysql

import requests

from bs4 import BeautifulSoup

定义爬取链接,%d用作数字占位:

baseUrl = "https://movie.douban.com/top250?start=%d&filter="

定义爬取数据方法:

def get_movies(start):

url = baseUrl % start # 拼接爬取链接

lists = [] # 存储此页面的电影数据

html = requests.get(url) # requests请求页面内容,由于豆瓣没有限制爬取,所以不用设置伪请求头

soup = BeautifulSoup(html.content, "html.parser") # BeautifulSoup解析页面内容

items = soup.find("ol", "grid_view").find_all("li") # 获取所有的电影内容

for i in items:

movie = {} # 临时存取电影的数据

movie["rank"] = i.find("em").text # 电影排行榜

movie["link"] = i.find("div","pic").find("a").get("href") # 电影详情页链接

movie["poster"] = i.find("div","pic").find("a").find('img').get("src") # 电影海报地址

movie["name"] = i.find("span", "title").text # 电影名字

movie["score"] = i.find("span", "rating_num").text # 电影评分

movie["quote"] = i.find("span", "inq").text if(i.find("span", "inq")) else "" # 某些电影没有点评,没有就设为空

lists.append(movie) # 保存到返回数组中

return lists

连接数据库并创建数据表:

连接数据库,需指定charset否则可能会报错

db = pymysql.connect(host="localhost",user="root",password="root",db="test",charset="utf8mb4")

cursor = db.cursor() # 创建一个游标对象

cursor.execute("DROP TABLE IF EXISTS movies") # 如果表存在则删除

创建表sql语句

createTab = """CREATE TABLE movies(

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(20) NOT NULL,

rank VARCHAR(4) NOT NULL,

link VARCHAR(50) NOT NULL,

poster VARCHAR(100) NOT NULL,

score VARCHAR(4) NOT NULL,

quote VARCHAR(50)

)"""

cursor.execute(createTab) # 执行创建数据表操作

......

db.close() # 关闭数据库

将提取到的数据存储到数据表中:

lists = get_movies(start) # 获取提取到数据

for i in lists:

# 插入数据到数据库sql语句,%s用作字符串占位

sql = "INSERT INTO `movies`(`name`,`rank`,`link`,`poster`,`score`,`quote`) VALUES(%s,%s,%s,%s,%s,%s)"

try:

cursor.execute(sql, (i["name"], i["rank"], i["link"], i["poster"], i["score"], i["quote"]))

db.commit()

print(i[0]+" is success")

except:

db.rollback()

start += 25

完整代码:

import pymysql

import requests

from bs4 import BeautifulSoup

baseUrl = "https://movie.douban.com/top250?start=%d&filter="

def get_movies(start):

url = baseUrl % start

lists = []

html = requests.get(url)

soup = BeautifulSoup(html.content, "html.parser")

items = soup.find("ol", "grid_view").find_all("li")

for i in items:

movie = {}

movie["rank"] = i.find("em").text

movie["link"] = i.find("div","pic").find("a").get("href")

movie["poster"] = i.find("div","pic").find("a").find('img').get("src")

movie["name"] = i.find("span", "title").text

movie["score"] = i.find("span", "rating_num").text

movie["quote"] = i.find("span", "inq").text if(i.find("span", "inq")) else ""

lists.append(movie)

return lists

if name == "__main__":

db = pymysql.connect(host="localhost",user="root",password="root",db="test",charset="utf8mb4")

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以使用 Python 的 requests、BeautifulSoup 和 pymysql 库来实现这个需求。 首先,我们需要请求豆瓣电影top250页面,获取页面的 HTML 文本。代码如下: ```python import requests from bs4 import BeautifulSoup url = 'https://movie.douban.com/top250' response = requests.get(url) html = response.text ``` 接着,我们需要使用 BeautifulSoup 对 HTML 文本进行解析,提取出我们需要的电影信息。代码如下: ```python soup = BeautifulSoup(html, 'html.parser') movies = [] for movie in soup.select('.item'): title = movie.select('.title')[0].text # 电影名称 score = movie.select('.rating_num')[0].text # 评分 info = movie.select('.bd p')[0].text.strip() # 电影信息 director_and_cast = info.split('\n')[0] # 导演和演员 year_and_region = info.split('\n')[-1] # 上映年份和地区 movies.append({ 'title': title, 'score': score, 'director_and_cast': director_and_cast, 'year_and_region': year_and_region, }) ``` 最后,我们需要把爬取到的电影信息存入数据库中。这里我们使用 pymysql 库来连接 MySQL 数据库,并将电影信息插入到名为 `movies` 的表中。代码如下: ```python import pymysql conn = pymysql.connect( host='localhost', user='root', password='password', database='test', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor ) with conn.cursor() as cursor: for movie in movies: sql = ''' INSERT INTO movies (title, score, director_and_cast, year_and_region) VALUES (%s, %s, %s, %s) ''' cursor.execute(sql, ( movie['title'], movie['score'], movie['director_and_cast'], movie['year_and_region'] )) conn.commit() ``` 完整代码如下: ```python import requests from bs4 import BeautifulSoup import pymysql url = 'https://movie.douban.com/top250' response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') movies = [] for movie in soup.select('.item'): title = movie.select('.title')[0].text # 电影名称 score = movie.select('.rating_num')[0].text # 评分 info = movie.select('.bd p')[0].text.strip() # 电影信息 director_and_cast = info.split('\n')[0] # 导演和演员 year_and_region = info.split('\n')[-1] # 上映年份和地区 movies.append({ 'title': title, 'score': score, 'director_and_cast': director_and_cast, 'year_and_region': year_and_region, }) conn = pymysql.connect( host='localhost', user='root', password='password', database='test', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor ) with conn.cursor() as cursor: for movie in movies: sql = ''' INSERT INTO movies (title, score, director_and_cast, year_and_region) VALUES (%s, %s, %s, %s) ''' cursor.execute(sql, ( movie['title'], movie['score'], movie['director_and_cast'], movie['year_and_region'] )) conn.commit() ``` 注意,这里的数据库连接信息需要根据实际情况进行修改。此外,还需要先在数据库中创建一个名为 `movies` 的表,用于存储电影信息。表结构如下: ```sql CREATE TABLE movies ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255), score FLOAT, director_and_cast VARCHAR(255), year_and_region VARCHAR(255) ); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值