爬取电影天堂并存入数据库

时间:2022-09-18 17:59:45

代码具体如下:

from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
from time import sleep
import re
import pymysql
import time


moviesLinks = set() #全局变量,来储存主网页的链接
conn = pymysql.connect(host = '127.0.0.1',port = 3306,user='root',passwd='None',db='mysql',charset = 'utf8')
cur = conn.cursor()

cur.execute('use movies')


def getLinks(pageUrl):
global moviesLinks
html = urlopen(pageUrl)
bs4 = BeautifulSoup(html,"xml")
for link in bs4.findAll("a",{"href":re.compile("/html/gndy/+[a-z]+/[0-9]+/[0-9]+\.html")}): #正则表达式选取电影链接(过滤掉游戏下载链接,动漫链接,综艺链接)
if link.attrs['href'] not in moviesLinks:
newLink = link.attrs['href']
moviesLinks.add(newLink)
getPageImformation(newLink)


def getPageImformation(pageUrl):
try:
url = 'http://www.dytt8.net/' + pageUrl
html = urlopen(url)
bs4 = BeautifulSoup(html, "xml")
name = bs4.find("div",{"id":"Zoom"}).p.get_text().split('◎')[1][4:].strip() #名字的处理
downloadLink = bs4.find("td",{"bgcolor":"#fdfddf"}).a.get_text()
print(name+ " "+downloadLink)
store(name,downloadLink)
except Exception:
print("一些页面不是影片介绍的页面而已,不用担心")
print("--------------------------------\n")

def store(name,downloadLink):
cur.execute('select * from Movie_heaven where name = %s',name)
row = cur.fetchone()
print(row)
if row == None:
cur.execute('insert into Movie_heaven(name,downloadLink) values (%s,%s)',(name,downloadLink))
cur.connection.commit()
else:
print('数据库里已经有了!')

while(True):
getLinks('http://www.dytt8.net/')
time.sleep(60)
cur.close()
conn.close()

# 3是年代
# 4是产地
# 5是类别
# 6是语言
# 7是字幕
# 8IMDb评分
#

下面是查询功能;


conn = pymysql.connect(host = '127.0.0.1',port = 3306,user='root',passwd='None',db='mysql',charset = 'utf8')
cur = conn.cursor()
cur.execute('use movies')
def search(name):
sql = "select * from Movie_heaven where name LIKE '%?%'"
sql = sql.replace("?",name)
cur.execute(sql)
print(cur.fetchone())


name = input("请输入你要查询的电影名字:")
search(name)
cur.close()
conn.close()




这里写代码片