这是我在CSDN的第一篇博客,假期刚自学Python,尝试爬取了一下豆瓣top250.
希望可以有大佬指点
感谢
import requests
from bs4 import BeautifulSoup
def gethtml(url):
try:
kv = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
r=requests.get(url,headers=kv,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
print("fault")
def jiexi(list1,list2,list3,html):
soup=BeautifulSoup(html,"html.parser")
div_list=soup.find_all('div', class_='hd')
div_list1=soup.find_all('div',class_='bd')
div_list2=soup.find_all('div',class_='star')
for each in div_list:
movie=each.a.span.text
list1.append(movie)
for each in div_list1:
yy=each.p.text.strip()
if len(yy)>2:
list2.append(yy)
for each in div_list2:
fen=each.contents
list3.append((fen[3]).text)
def printlist(list1,list2,list3):
print("豆瓣排名电影")
print(len(list1))
for i in range(len(list1)):
print("排名"+str(i+1))
print("名字:"+list1[i])
print("详情:"+list2[i])
print("评分:"+list3[i])
print("--------------------------------------------------------------------------")
def main():
url0="https://movie.douban.com/top250?start="
list1=[]
list2=[]
list3=[]
for i in range(4): #4是爬取的页数
try:
url=url0+str(i*25)
html=gethtml(url)
jiexi(list1,list2,list3,html)
except:
continue
printlist(list1,list2,list3)
main()