(2)python 파충류 실례: 고양이 눈 영화 TOP 100 랭킹과 영화 정보를 Excel에 기록(Excel 열 너비 적응)

9729 단어
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
import xlrd,xlwt

urls = [
    "https://maoyan.com/board/4?offset={}".format(i)
    for i in range(0,100,10)
]

header = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ("
              "KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
}
length = {}



def len_byte(value):
    length = len(value)
    utf8_length = len(value.encode('utf-8'))
    length = (utf8_length - length)/2 + length
    return int(length)

#         
def FilmInformation(url):
    content = []
    r = requests.get(url,headers = header)
    respond = r.text
    soup = BeautifulSoup(respond,"html.parser")
    films = soup.select(".board-item-main")
    for film in films:
        name = (film.select("[title]")[0].text)
        # name = (film.select(".name a")[0].text)    0 , 
        staring = (film.select(".star")[0].text).strip().split("")[1]  # 
        releasetime = (film.select(".releasetime")[0].text).split("")[1].split("(")[0]
        country = (film.select(".releasetime")[0].text).split("")[1][10:]
        if country:
            country = country.replace("(","").replace(")","") #lstrip() rstip() 
        else:
            country = "( )"
        integer = (film.select(".integer")[0].text)
        fraction = (film.select(".fraction")[0].text)
        score = integer + fraction
        content.append([name,staring,releasetime,country,score])
    return content

def WriteExcel(data):
    global length
    title = [" "," "," "," "," "]
    workbook = xlwt.Workbook(encoding = "utf-8")
    sheet = workbook.add_sheet(" 100")
    row = 1
    for i in range(len(title)):
        sheet.write(0,i,title[i])
    for num in data:
        for num_num in num:
            #for num_num_num in num_num:
            for col in range(len(num_num)):
                sheet.write(row,col,num_num[col])
                if col in length:
                    if length[col] < len(num_num[col]):
                        length[col] = len(num_num[col])
                else:
                    length.setdefault(col, len(num_num[col]))
            row +=1
    for key,value in length.items():
        sheet.col(key).width = int(256*value*2)

    workbook.save("maoyan.xls")



def main():
    all = []
    for url in urls:
        result = FilmInformation(url)
        all.append(result)
    WriteExcel(all)

if __name__ =="__main__":
    main()
    print (length)

좋은 웹페이지 즐겨찾기