脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python爬取豆瓣电影TOP250数据

python爬取豆瓣电影TOP250数据

2021-11-10 10:38秋无之地 Python

这次以豆瓣电影TOP250网为例编写一个爬虫程序,并将爬取到的数据(排名、电影名和电影海报网址)存入MySQL数据库中。

在执行程序前,先在MySQL中创建一个数据库"pachong"。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pymysql
import requests
import re
 
 
#获取资源并下载
def resp(listURL):
    #连接数据库
    conn = pymysql.connect(
        host = '127.0.0.1',
        port = 3306,
        user = 'root',
        password = '******'#数据库密码请根据自身实际密码输入
        database = 'pachong',
        charset = 'utf8'
    )
 
    #创建数据库游标
    cursor = conn.cursor()
 
    #创建列表t_movieTOP250(执行sql语句)
    cursor.execute('create table t_movieTOP250(id INT PRIMARY KEY                                               auto_increment NOT NULL ,movieName VARCHAR(20) NOT NULL                                     ,pictrue_address VARCHAR(100))')
 
    try:
        # 爬取数据
        for urlPath in listURL:
            # 获取网页源代码
            response = requests.get(urlPath)
            html = response.text
 
            # 正则表达式
            namePat = r'alt="(.*?)" src='
            imgPat = r'src="(.*?)" class='
 
            # 匹配正则(排名【用数据库中id代替,自动生成及排序】、电影名、电影海报(图片地址))
            res2 = re.compile(namePat)
            res3 = re.compile(imgPat)
            textList2 = res2.findall(html)
            textList3 = res3.findall(html)
 
            # 遍历列表中元素,并将数据存入数据库
            for i in range(len(textList3)):
                cursor.execute('insert into t_movieTOP250(movieName,pictrue_address)                                    VALUES("%s","%s")' % (textList2[i],textList3[i]))
 
        #从游标中获取结果
        cursor.fetchall()
 
        #提交结果
        conn.commit()
        print("结果已提交")
 
    except Exception as e:
        #数据回滚
        conn.rollback()
        print("数据已回滚")
 
    #关闭数据库
    conn.close()
 
#top250所有网页网址
def page(url):
    urlList = []
    for i in range(10):
        num = str(25*i)
        pagePat = r'?start=' + num + '&filter='
        urL = url+pagePat
        urlList.append(urL)
    return urlList
 
 
if __name__ == '__main__':
    url = r"https://movie.douban.com/top250"
    listURL = page(url)
    resp(listURL)

结果如下图:

python爬取豆瓣电影TOP250数据

python爬取豆瓣电影TOP250数据

以上就是我的分享,如果有什么不足之处请指出,多交流,谢谢!

以上就是python爬取豆瓣电影TOP250数据的详细内容,更多关于python爬取豆瓣电影的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/qiuwuzhidi/p/14784302.html

延伸 · 阅读

精彩推荐