爬虫练习 用beautifulsoup 爬取猫眼top100

import requests
from bs4 import BeautifulSoup as bs
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1;Win64;x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/55.0.2883.87 Safari/537.36'}

for i in range(10):
    items=[]
    resp=requests.get('https://maoyan.com/board/4?offset='+str(i*10),headers=headers)
    html=resp.text
    soup=bs(html,'lxml')
    item=soup.find_all('dd')
    items.extend(item)
    for film in items:
        dict={}
        dict['index']=film.i.string
        dict['name']=film.a.attrs['title']#film.find(name='p',attrs={'class':'name'}).string.strip()
        dict['star']=film.find(name='p',attrs={'class':'star'}).string.strip()
        dict['time']=film.find(name='p',attrs={'class':'releasetime'}).string.strip()
        socre1=film.find(name='i',attrs={'class':'integer'}).string
        score2=film.find(name='i',attrs={'class':'fraction'}).string
        dict['score']=score1+score2
        with open('film','a',encoding='utf-8')as f:
             f.write(str(dict)+'\n')

 

你可能感兴趣的:(爬虫)