python3 爬取豆瓣电影TOP250,漂亮表格彩色显示

#coding=utf-8  
import requests
import re
from bs4 import BeautifulSoup
from prettytable import PrettyTable
from colorama import Fore,Style
 

def getHtml(url):  
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0'}  
    page = requests.get(url,headers = headers)
    html =page.text
    return html

if __name__=='__main__':
    table = PrettyTable(['NO.','movie','year','star','vote'])
    movies = []
    years = []
    stars = []
    votes = []
    for page in range(0,250,25):
        url = 'https://movie.douban.com/top250?start={0}&filter='.format(page)
        Html = getHtml(url)
        Soup = BeautifulSoup(Html,'html.parser')
        names = Soup.find_all('div',class_ = 'info')
        
        for name in names:
            movie = name.find_all('span',class_ = 'title')
            movies.append(movie[0].text)
        tags = Soup.find_all('p',class_ = '')
        for tag in tags:
            tag = tag.text
            reg = r'\d+'
            tag = re.findall(reg,tag)
            years.append(tag[0])

        tags = Soup.find_all('div',class_ = 'star')
        for tag in tags:
            star = tag.find_all('span')
            stars.append(star[1].text)
            reg = r'\d+'
            vote = re.findall(reg,star[3].text)
            votes.append(vote[0])
    for i in range(0,250):
        table.add_row([Fore.RED + str(i+1),Style.BRIGHT + Fore.GREEN + movies[i],Fore.YELLOW +years[i],Fore.BLUE +stars[i],Fore.CYAN +votes[i]])
    print (table)

效果图如下:

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值