数据获取方式:微信搜索关注【靠谱杨阅读人生】回复【电影】。
整理不易,资源付费,谢谢支持。

代码:

  1 import time
2 import traceback
3 import requests
4 from lxml import etree
5 import re
6 from bs4 import BeautifulSoup
7 from lxml.html.diff import end_tag
8 import json
9 import pymysql
10 #连接数据库 获取游标
11 def get_conn():
12 """
13 :return: 连接,游标
14 """
15 # 创建连接
16 conn = pymysql.connect(host="127.0.0.1",
17 user="root",
18 password="000429",
19 db="movierankings",
20 charset="utf8")
21 # 创建游标
22 cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
23 if ((conn != None) & (cursor != None)):
24 print("数据库连接成功!游标创建成功!")
25 else:
26 print("数据库连接失败!")
27 return conn, cursor
28 #关闭数据库连接和游标
29 def close_conn(conn, cursor):
30 if cursor:
31 cursor.close()
32 if conn:
33 conn.close()
34 return 1
35
36 def get_souhu():
37 url='https://film.sohu.com/list_0_0_0_2_2_1_60.html?channeled=1200100000'
38 #最新上架
39 new_url='https://film.sohu.com/list_0_0_0_2_1_1_60.html?channeled=1200100000'
40 #本周热播
41 week_url='https://film.sohu.com/list_0_0_0_2_0_1_60.html?channeled=1200100000'
42 headers={
43 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'
44 }
45
46 #初始化list
47 templist=[]
48 dataRes=[]
49 #最受好评
50 for i in range(1,31):
51 url_1='https://film.sohu.com/list_0_0_0_2_2_'
52 auto=str(i)
53 url_2='_60.html?channeled=1200100000'
54 url=url_1+auto+url_2
55 response = requests.get(url, headers)
56 response.encoding = 'utf-8'
57 page_text = response.text
58 # etree_ = etree.HTML(page_text)
59 # 获取所有的li
60 soup = BeautifulSoup(page_text, 'lxml')
61 # 标签层级选择
62 li_list = soup.select('.movie-list>li')
63 print(len(li_list))
64 if(len(li_list)==0):
65 print("最受好评爬取结束!")
66 if(len(dataRes)!=0):
67 return dataRes
68 for li in li_list:
69 li_text=str(li)
70 # print(li_text)
71 li_soup=BeautifulSoup(li_text,'lxml')
72 name=li_soup.find('div',class_="v_name_info").text
73 #添加名字
74 templist.append(name)
75 # print(name)
76 #添加评分
77 score=li_soup.find('span',class_='v_score').text
78 #处理评分
79 score=score[-4:-1]
80 templist.append(score)
81 # print(score)
82 #添加path
83 path=li_soup.find('a',target="_blank")['href']
84 templist.append(path)
85 # print(path)
86 #添加播放状态
87 state="VIP"
88 templist.append(state)
89 print(templist)
90 dataRes.append(templist)
91 templist=[]
92 print("-------------------------------------------")
93 # print(len(dataRes))
94
95 # #最新上架
96 #
97 # templist = []
98 # for i in range(1,31):
99 # url_1='https://film.sohu.com/list_0_0_0_2_1_'
100 # auto=str(i)
101 # url_2='_60.html?channeled=1200100000'
102 # url=url_1+auto+url_2
103 # response = requests.get(url, headers)
104 # response.encoding = 'utf-8'
105 # page_text = response.text
106 # # etree_ = etree.HTML(page_text)
107 # # 获取所有的li
108 # soup = BeautifulSoup(page_text, 'lxml')
109 # # 标签层级选择
110 # li_list = soup.select('.movie-list>li')
111 # print(len(li_list))
112 # if(len(li_list)==0):
113 # print("最新上架爬取结束!")
114 # if(len(dataRes)!=0):
115 # return dataRes
116 # for li in li_list:
117 # li_text=str(li)
118 # # print(li_text)
119 # li_soup=BeautifulSoup(li_text,'lxml')
120 # name=li_soup.find('div',class_="v_name_info").text
121 # #添加名字
122 # templist.append(name)
123 # # print(name)
124 # #添加评分
125 # score=li_soup.find('span',class_='v_score').text
126 # #处理评分
127 # score=score[-4:-1]
128 # templist.append(score)
129 # # print(score)
130 # #添加path
131 # path=li_soup.find('a',target="_blank")['href']
132 # templist.append(path)
133 # # print(path)
134 # #添加播放状态
135 # state="VIP"
136 # templist.append(state)
137 # print(templist)
138 # dataRes.append(templist)
139 # templist=[]
140 # print("-------------------------------------------")
141 # # print(len(dataRes))
142 # #本周热播
143 # templist = []
144 # for i in range(1, 31):
145 # url_1 = 'https://film.sohu.com/list_0_0_0_2_0_'
146 # auto = str(i)
147 # url_2 = '_60.html?channeled=1200100000'
148 # url = url_1 + auto + url_2
149 # response = requests.get(url, headers)
150 # response.encoding = 'utf-8'
151 # page_text = response.text
152 # # etree_ = etree.HTML(page_text)
153 # # 获取所有的li
154 # soup = BeautifulSoup(page_text, 'lxml')
155 # # 标签层级选择
156 # li_list = soup.select('.movie-list>li')
157 # print(len(li_list))
158 # if (len(li_list) == 0):
159 # print("本周热播爬取结束!")
160 # if (len(dataRes) != 0):
161 # return dataRes
162 # for li in li_list:
163 # li_text = str(li)
164 # # print(li_text)
165 # li_soup = BeautifulSoup(li_text, 'lxml')
166 # name = li_soup.find('div', class_="v_name_info").text
167 # # 添加名字
168 # templist.append(name)
169 # # print(name)
170 # # 添加评分
171 # score = li_soup.find('span', class_='v_score').text
172 # # 处理评分
173 # score = score[-4:-1]
174 # templist.append(score)
175 # # print(score)
176 # # 添加path
177 # path = li_soup.find('a', target="_blank")['href']
178 # templist.append(path)
179 # # print(path)
180 # # 添加播放状态
181 # state = "VIP"
182 # templist.append(state)
183 # print(templist)
184 # dataRes.append(templist)
185 # templist = []
186 # print("-------------------------------------------")
187 # print(len(dataRes))
188 #list去重
189 # old_list = dataRes
190 # new_list = []
191 # for i in old_list:
192 # if i not in new_list:
193 # new_list.append(i)
194 # print(new_list) # [2, 3, 4, 5, 1]
195 return dataRes
196 #插入数据库
197 def insert_souhu():
198 cursor = None
199 conn = None
200 try:
201 count=0
202 list = get_souhu()
203 print(f"{time.asctime()}开始插入搜狐电影数据")
204 conn, cursor = get_conn()
205 sql = "insert into moviesohu (id,name,score,path,state) values(%s,%s,%s,%s,%s)"
206 for item in list:
207 print(item)
208 count = count + 1
209 #异常捕获,防止数据库主键冲突
210 try:
211 cursor.execute(sql, [0, item[0], item[1], item[2], item[3] ])
212 except pymysql.err.IntegrityError:
213 print("重复!跳过!")
214 conn.commit() # 提交事务 update delete insert操作
215 print(f"{time.asctime()}插入搜狐电影数据完毕")
216 except:
217 traceback.print_exc()
218 finally:
219 close_conn(conn, cursor)
220 return;
221
222 if __name__ == '__main__':
223 # get_iqy()
224 # get_souhu()
225 insert_souhu()

运行截图

数据库截图

建表语句

1 CREATE TABLE `moviesohu` (
2 `id` INT(11) NOT NULL AUTO_INCREMENT,
3 `name` VARCHAR(45) COLLATE utf8_bin NOT NULL,
4 `score` VARCHAR(45) COLLATE utf8_bin NOT NULL,
5 `path` VARCHAR(100) COLLATE utf8_bin NOT NULL,
6 `state` VARCHAR(10) COLLATE utf8_bin NOT NULL,
7 PRIMARY KEY (`name`),
8 KEY `id` (`id`)
9 ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

Python爬虫爬取搜狐视频电影并存储到mysql数据库的更多相关文章

  1. python爬虫:爬取慕课网视频

    前段时间安装了一个慕课网app,发现不用注册就可以在线看其中的视频,就有了想爬取其中的视频,用来在电脑上学习.决定花两天时间用学了一段时间的python做一做.(我的新书<Python爬虫开发与 ...

  2. Python爬虫 - 爬取百度html代码前200行

    Python爬虫 - 爬取百度html代码前200行 - 改进版,  增加了对字符串的.strip()处理 源代码如下: # 改进版, 增加了 .strip()方法的使用 # coding=utf-8 ...

  3. 用Python爬虫爬取广州大学教务系统的成绩(内网访问)

    用Python爬虫爬取广州大学教务系统的成绩(内网访问) 在进行爬取前,首先要了解: 1.什么是CSS选择器? 每一条css样式定义由两部分组成,形式如下: [code] 选择器{样式} [/code ...

  4. 使用Python爬虫爬取网络美女图片

    代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install ...

  5. Python爬虫|爬取喜马拉雅音频

    "GOOD Python爬虫|爬取喜马拉雅音频 喜马拉雅是知名的专业的音频分享平台,用户规模突破4.8亿,汇集了有声小说,有声读物,儿童睡前故事,相声小品等数亿条音频,成为国内发展最快.规模 ...

  6. python爬虫爬取内容中,-xa0,-u3000的含义

    python爬虫爬取内容中,-xa0,-u3000的含义 - CSDN博客 https://blog.csdn.net/aiwuzhi12/article/details/54866310

  7. Python爬虫爬取全书网小说,程序源码+程序详细分析

    Python爬虫爬取全书网小说教程 第一步:打开谷歌浏览器,搜索全书网,然后再点击你想下载的小说,进入图一页面后点击F12选择Network,如果没有内容按F5刷新一下 点击Network之后出现如下 ...

  8. python爬虫—爬取英文名以及正则表达式的介绍

    python爬虫—爬取英文名以及正则表达式的介绍 爬取英文名: 一.  爬虫模块详细设计 (1)整体思路 对于本次爬取英文名数据的爬虫实现,我的思路是先将A-Z所有英文名的连接爬取出来,保存在一个cs ...

  9. 一个简单的python爬虫,爬取知乎

    一个简单的python爬虫,爬取知乎 主要实现 爬取一个收藏夹 里 所有问题答案下的 图片 文字信息暂未收录,可自行实现,比图片更简单 具体代码里有详细注释,请自行阅读 项目源码: # -*- cod ...

  10. python爬虫-爬取百度图片

    python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...

随机推荐

  1. Java集合框架学习(十三) Collections类详解

    Collections类介绍 这个类操作或返回集合的专有静态方法. 它包含多态算法,利用wrappers(封装器)返回指定集合类型的新集合,以及其他一些零散功能. 如果该类的方法引用的集合或类对象为n ...

  2. golang中协程&管道&锁

    进程和线程 进程(Process)就是程序在操作系统中的一次执行过程,是系统进行资源分配和调度的基 本单位,进程是一个动态概念,是程序在执行过程中分配和管理资源的基本单位,每一个进 程都有一个自己的地 ...

  3. click模块解析命令行参数

    click模块解析命令行参数 安装 pip install click 操作步骤 1)使用@click.command()装饰一个函数,使之成为命令行接口 2)使用@click.option()等装饰 ...

  4. day03--实际操作演示linux系统挂载过程

    # 第一步骤: 拥有一个存储设备-光驱,使光驱加载光盘 # 第二步骤: 在linux系统中找到光驱设备 ls -l /dev/cdrom # 第三步骤: 需要将存储设备进行 挂载 挂载命令语法格式: ...

  5. 高效的PDF文字提取技术

    无论是行政法规.学术论文还是企业合同,PDF文档为我们提供了一种便捷.稳定的信息传递方式.然而,从PDF文件中提取文本信息对于数据分析.内容编辑等后续处理来说至关重要. PDF文本提取技术是一种可以从 ...

  6. 【Azure Redis 缓存】Redis的监控方式? 是否有API接口调用来获取监控值

    问题描述 对于PaaS的Azure Cache for Redis,Azure中有哪些监控方式?是否能有api接口调用来获取监控值? 问题答案 1) 在Redis的门户中,使用Metrics查看Red ...

  7. 【Azure 应用服务】如何查看App Service Java堆栈JVM相关的参数默认配置值?

    问题描述 如何查看App Service Java堆栈JVM相关的参数默认配置值? 问题解答 可以通过App Service的高级管理工具(kudu:)来查看JVM的相关参数,使用命令:java -X ...

  8. Jupyter Notebook 遇上 NebulaGraph,可视化探索图数据库

    在之前的<手把手教你用 NebulaGraph AI 全家桶跑图算法>中,除了介绍了 ngai 这个小工具之外,还提到了一件事有了 Jupyter Notebook 插件: https:/ ...

  9. uni-app实现公众号登陆实现

    公众号实现登陆流程思路: 1. 创建一个页面用于登陆,页面上需要有输入账号和密码的表单,以及登陆按钮.2. 在登陆按钮的点击事件中,调用后端接口进行账号密码校验.如果校验通过,则将后端返回的用户信息保 ...

  10. 辨析Java与网络通信中的编码与解码

    在Java字符流上下文中的编码和解码,以及在网络通信中的编码概念. 在Java中,当我们谈论字符流(如Reader和Writer)时,编码和解码主要涉及将字符数据转换为字节数据,以及将字节数据转换回字 ...