求助大佬们为什么我爬出来的只有一页数据
from bs4 import BeautifulSoupimport 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)
data.append(link)
imgSrc = re.findall(findImgSrc,item)
data.append(imgSrc)
titles = re.findall(findTitle,item)
if(len(titles) == 2):
ctitle = titles
data.append(ctitle)
otitle = titles.replace("/","")
data.append(otitle)
else:
data.append(titles)
data.append(' ')
rating = re.findall(findRating,item)
data.append(rating)
judeNum = re.findall(findJudge,item)
data.append(judeNum)
inq = re.findall(findInq,item)
if len(inq) != 0:
inq = inq.replace(".","")
data.append(inq)
else:
data.append(" ")
bd = re.findall(findBd,item)
bd=bd
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)==type([]):
data = '"' + str(data) + '"'
else:
data = '"'+data+'"'
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 integerprimarykeyautoincrement,
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("爬取完毕!") baseurl = "https://movie.douban.com/top250?Start=0"
改成:
baseurl = "https://movie.douban.com/top250?start=0"
这样试试 代码片段1改成:
def main():
baseurl = "https://movie.douban.com/top250?start={}&filter="
datalist = getData(baseurl)
# savepath = ".\\豆瓣TOP250.xls"
dbpath = "movie2.db"
# saveData(savepath)
saveData2DB(datalist,dbpath)
代码片段2改成:
def getData(baseurl):
datalist = []
for i in range(0,11):
url = baseurl.format(i*25)
html = askURL(url)
jtxs0000 发表于 2021-1-4 14:04
不行。。什么都没这样 z5560636 发表于 2021-1-4 16:23
代码片段1改成:
也不行 什么都爬不出来了
可以了缩进问题 谢谢大家 wwf 发表于 2021-1-4 21:43
可以了缩进问题 谢谢大家
请把【问题求助】改为【已经解决】
页:
[1]