美文网首页
python第四天(一)BeautifulSoup爬虫

python第四天(一)BeautifulSoup爬虫

作者: 敏姐姐_e9d0 | 来源:发表于2018-10-31 13:29 被阅读0次

    爬取猫眼top100数据

    import requests
    from bs4 import BeautifulSoup
    import time
    
    
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
    }
    
    # url = 'http://maoyan.com/board/4'
    
    def get_info(url):
        res = requests.get(url, headers=headers)#请求网页
        soup = BeautifulSoup(res.text,'html.parser')#解析网页
        ranks = soup.select('div.main > dl > dd > i')#排名
        names = soup.select('div.movie-item-info > p.name > a')#电影名
        actors = soup.select('div.movie-item-info > p.star')#主演
        play_times = soup.select('div.movie-item-info > p.releasetime')#播放时长
        scores_1 = soup.select('div.movie-item-number.score-num > p > i.integer')#评分
        scores_2 = soup.select('div.movie-item-number.score-num > p > i.fraction')#评分
    
        for rank, name, actor, play_time, score_1, score_2 in zip(ranks,names,actors,play_times,scores_1, scores_2):
            print(rank.get_text(),name.get_text(),actor.get_text().strip(),play_time.get_text(),score_1.get_text()+score_2.get_text())
    
    
    
    if __name__ == '__main__':
        urls = ['http://maoyan.com/board/4?offset={}'.format(str(i)) for i in range(0,100,10)]#多页提取
    

    爬取酷狗top500数据

    import requests
    from bs4 import BeautifulSoup
    
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    
    url = 'http://www.kugou.com/yy/rank/home/1-8888.html'
    res = requests.get(url,headers=headers)
    soup = BeautifulSoup(res.text,'html.parser')
    song_singers = soup.select('div.pc_temp_songlist > ul > li > a')
    long_times = soup.select('#rankWrap > div.pc_temp_songlist > ul > li > span.pc_temp_tips_r > span')
    ranks = soup.select('#rankWrap > div.pc_temp_songlist > ul > li > span.pc_temp_num')
    # print(rank)
    for song_singer,long_time,rank in zip(song_singers,long_times,ranks):
        print(song_singer.get_text(),long_time.get_text().strip(),rank.get_text().strip())
    

    相关文章

      网友评论

          本文标题:python第四天(一)BeautifulSoup爬虫

          本文链接:https://www.haomeiwen.com/subject/tecatqtx.html