|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from bs4 import BeautifulSoup
import re
import urllib.request
import xlwt
import sqlite3
def main():
baseurl = "https://movie.douban.com/top250?Start=0"
datalist = getData(baseurl)
# savepath = ".\\豆瓣TOP250.xls"
dbpath = "movie2.db"
# saveData(savepath)
saveData2DB(datalist,dbpath)
# askURL("https://movie.douban.com/subject/30482645/?from=0")
findLink = re.compile(r'<a href="(.*?)">')
# findImgSrc = re.compile(r'<img.*src=“(.*?)”',re.S)
findImgSrc = re.compile(r'<img.*src="(.*)" width=',re.S)
findTitle = re.compile(r'<span class="title">(.*)</span>')
findRating = re.compile(r'<span class="rating_num" property="v:average">(.*)</span>')
findJudge = re.compile(r'<span>(\d*)人评价</span>')
findInq = re.compile(r'<span class="inq">(.*)</span>')
findBd = re.compile(r'<p class="">(.*?)</p>',re.S)
def getData(baseurl):
datalist = []
for i in range(0,11):
url = baseurl + str(i*25)
html = askURL(url)
soup = BeautifulSoup(html,"html.parser")
for item in soup.find_all('div',class_="item"):
# print(item)
data = []
item = str(item)
link = re.findall(findLink,item)[0]
data.append(link)
imgSrc = re.findall(findImgSrc,item)[0]
data.append(imgSrc)
titles = re.findall(findTitle,item)
if(len(titles) == 2):
ctitle = titles[0]
data.append(ctitle)
otitle = titles[1].replace("/","")
data.append(otitle)
else:
data.append(titles[0])
data.append(' ')
rating = re.findall(findRating,item)[0]
data.append(rating)
judeNum = re.findall(findJudge,item)[0]
data.append(judeNum)
inq = re.findall(findInq,item)
if len(inq) != 0:
inq = inq[0].replace(".","")
data.append(inq)
else:
data.append(" ")
bd = re.findall(findBd,item)
bd=bd[0]
bd = re.sub('<br(\s+)?/>(\s+)?'," ",bd)
bd = re.sub('/'," ",bd)
data.append(bd.strip())
datalist.append(data)
return datalist
def askURL(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0',
}
request = urllib.request.Request(url, headers=headers)
page = urllib.request.urlopen(request)
html = page.read()
try:
response = urllib.request.urlopen(request)
html = response.read().decode("utf-8")
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
return html
def saveData(savepath):
print("save....")
def saveData2DB(datalist,dbpath):
init_db(dbpath)
conn = sqlite3.connect(dbpath)
cur = conn.cursor()
for data in datalist:
for index in range(len(data)):
if type(data[index])==type([]):
data[index] = '"' + str(data[index]) + '"'
else:
data[index] = '"'+data[index]+'"'
sql = '''
insert into movie250 (
info_link,pic_link,cname,ename,score,rated,instroduction,info)
values (%s)'''%",".join(data)
print(sql)
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
def init_db(dbpath):
sql = '''
create table movie250
(
id integer primary key autoincrement,
info_link text,
pic_link text,
cname varchar,
ename varchar,
score numeric,
rated numric,
instroduction text,
info text
)
'''
conn = sqlite3.connect(dbpath)
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
conn.close()
if __name__=="__main__":
main()
# init_db("movietest.db")
print("爬取完毕!") |
|