1.Top100榜
import pymysql as mysql
import requests
from bs4 import BeautifulSoup
def top():
# 创建列表,存储片名、上映时间、主演、评分
list1 = []
list2 = []
list3 = []
list4 = []
# 二维数组删除前面的数据,保证list输出之前是不多余的100个数据
list1.clear()
list2.clear()
list3.clear()
# 请求
resp = requests.get(url=url_one,headers=head,timeout=time)
if resp.status_code == 200:
resp.encoding = "utf-8"
soup = BeautifulSoup(resp.text, "html.parser")
div_list = soup.find_all(name="div", attrs={"class": "movie-item-info"})
scorelist = soup.find_all(name="div",attrs={"class":"movie-item-number score-num"})
# 获取:片名、上映时间、主演
for div in div_list:
p = div.find_all(name="p") # 返回一个列表
list1.append(p[0].string)
list2.append(p[1].string.replace(" ", ""))
list3.append(p[2].string)
print(list1)
# 获取:评分
for score in scorelist:
s = score.find_all(name="i")
s0 = s[0].string
s1 = s[1].string
sAdd = s0 + s1
list4.append(sAdd)
# print(list4)
else:
print("错误!重新开始")
top()
# 存储到数据库中
for i in range(0,10):
try:
i1 = cur.execute("INSERT INTO Top100(`片名`,`主演`,`上映时间`,`评分`) VALUES('%s','%s','%s','%s')" %(
list1[i], list2[i], list3[i], list4[i]))
dbHelp.commit()
# print(i1)
except:
dbHelp.rollback()
if __name__ == '__main__':
# 连接数据库
host = "localhost" # 127.0.0.1=本机IP
user = "root"
password = "123456"
db = "maoYan" # 数据库
port = 3306 # number类型
dbHelp = mysql.connect(host=host, user=user, password=password, db=db, port=port)
print("数据库连接成功!")
cur = dbHelp.cursor() # 获取游标,使用sql语句
# 访问网络
offset = "0"
for i in range(11):
offset = i * 10
url_