您的位置:首页技术文章
文章详情页

python爬取豆瓣电影TOP250数据

【字号: 日期:2022-06-18 16:25:24浏览:2作者:猪猪

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

import pymysqlimport requestsimport 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='https://www.xxx.com.cn/bcjs/(.*?)' 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+pagePaturlList.append(urL) return urlListif __name__ == ’__main__’: url = r'https://movie.douban.com/top250' listURL = page(url) resp(listURL)

结果如下图:

python爬取豆瓣电影TOP250数据

python爬取豆瓣电影TOP250数据

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

以上就是python爬取豆瓣电影TOP250数据的详细内容,更多关于python爬取豆瓣电影的资料请关注好吧啦网其它相关文章!

标签: 豆瓣 Python
相关文章: