欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

爬虫练习 用beautifulsoup 爬取猫眼top100

程序员文章站 2022-05-02 17:06:25
...
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')