欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

猫眼电影MySQL数据库怎么写_Python3爬取猫眼电影榜并将数据存入MySql

发布时间:2023/12/14 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 猫眼电影MySQL数据库怎么写_Python3爬取猫眼电影榜并将数据存入MySql 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

直接上代码:

#coding=utf-8

import re

import time

import pymysql

import requests

from requests.exceptions import RequestException

from bs4 import BeautifulSoup

def setMySql():

db = pymysql.connect(host = 'localhost',user = 'root',password = '123456',port = 3306)

cursor = db.cursor() #设置游标

sql = "CREATE DATABASE movies DEFAULT CHARACTER SET utf8" #新建一个movies数据库

cursor.execute(sql)

db = pymysql.connect(host = 'localhost',user = 'root',password = '123456',port = 3306,db = "movies")

cursor = db.cursor() #设置游标

sql = "CREATE TABLE IF NOT EXISTS movies(movie VARCHAR(255) NOT NULL,actor VARCHAR(255) NOT NULL,score VARCHAR(255) NOT NULL,realse VARCHAR(255) NOT NULL,PRIMARY KEY(movie))"

cursor.execute(sql)

db.close()

def getHtml(url):

try:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0'}

response = requests.get(url,headers = headers)

if response.status_code == 200:

return response.text

return None

except RequestException:

return None

def getdata():

lmovie = []

lactor = []

ltime = []

lscore = []

index = 0

for page in range(0,100,10):

url = 'http://maoyan.com/board/4?offset={}'.format(page)

Html = getHtml(url)

if Html is None:

contine

Soup = BeautifulSoup(Html,'lxml')

names = Soup.select('div.movie-item-info > p:nth-of-type(1)')

for name in names:

lmovie.append(name.text)

actors = Soup.select('div.movie-item-info > p:nth-of-type(2)')

for actor in actors:

actor = actor.text

actor = actor.split(':')[-1]

lactor.append(actor.strip())

releasetimes = Soup.select('div.movie-item-info > p:nth-of-type(3)')

for retime in releasetimes:

retime = retime.text

retime = retime.split(':')[-1]

ltime.append(retime)

scores = Soup.select('p.score > i:nth-of-type(1)')

for score in scores:

lscore.append(score.text)

frascores = Soup.select('p.score > i:nth-of-type(2)')

for i in range(len(frascores)):

lscore[index] += frascores[i].text

index += 1

time.sleep(1)

db = pymysql.connect(host = 'localhost',user = 'root',password = '123456',port = 3306,db = "movies")

cursor = db.cursor() #设置游标

sql = "INSERT INTO movies(movie,actor,score,realse) values(%s,%s,%s,%s)"

for data in zip(lmovie,lactor,lscore,ltime):

try:

cursor.execute(sql,data)

db.commit()

except:

db.rollback()

db.close()

if __name__=='__main__':

setMySql() 运行一次即可

getdata()

结果如下图:

总结

以上是生活随笔为你收集整理的猫眼电影MySQL数据库怎么写_Python3爬取猫眼电影榜并将数据存入MySql的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。