鱼C论坛

 找回密码
 立即注册
查看: 1595|回复: 3

[已解决]怎么把豆瓣爬到的数据放进去一个表格里啊

[复制链接]
发表于 2021-4-6 23:18:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import requests
import bs4
import openpyxl
import os
os.chdir("D:\BianCheng\Pythonwork\爬虫爬出来资料")

header ={}
header['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'

def open_url(url):
    res = requests.get(url, headers=header)
    return res

def look_movie(res):
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    targets = soup.find_all("div", class_='hd')
    list1 = []
    for each in targets:
        list1.append(each.a.span.text)
    return list1

def main():
    yeshu = 0

    while yeshu <= 25:
        url = "http://movie.douban.com/top250?start=" + str(yeshu) + "&filter="
        res = open_url(url)
        data = look_movie(res)
        yeshu += 25
        print(data)
    # to_excel(data)这个就是存表格的,但运行它就报错,不运行注释了就正常...

def to_excel(data):
    wb = openpyxl.Workbook()
    wb.guess_types = True
    ws = wb.active
    ws.append(['1','2','3','4','5'])
    for each in data:
        ws.append(each)

    wb.save("豆瓣250电影榜.xlsx")

if __name__ == '__main__':
    main()
我根据极客爬虫篇里摘取出一些可以用的,但就是用不了现在,希望有小伙伴解救一下谢谢
最佳答案
2021-4-7 00:00:32
终于帮你改完了 满意请给最佳
2021-04-06_23-59-55 by Sharpstar_kylin.png
2021-04-07_00-00-05 by Sharpstar_kylin.png
import requests
import bs4
import openpyxl

import os
# os.chdir("D:\BianCheng\Pythonwork\爬虫爬出来资料")

header ={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}

def open_url(url):
    res = requests.get(url, headers=header)
    return res


def look_movie(res):
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    targets = soup.find_all("div", class_='hd')
    list1 = []
    for each in targets:
        list1.append(each.a.span.text)
    return list1


def main():
    yeshu = 0

    data = []
    while yeshu <= 225:
        url = "http://movie.douban.com/top250?start=" + str(yeshu) + "&filter="
        res = open_url(url)
        data.extend(look_movie(res))
        yeshu += 25
    to_excel(data, yeshu)


def to_excel(data, yeshu):
    wb = openpyxl.Workbook()
    wb.guess_types = True
    ws = wb.active
    data_rank = [str(i) for i in range(yeshu+1-len(data), yeshu+1)]
    ws.append(data_rank)
    for movie_rank,movie_name in zip(data_rank, data):
        print("%s 排名:%s" % (movie_name, movie_rank))
    print(data)
    ws.append(data)
    wb.save("豆瓣250电影榜.xlsx")


if __name__ == '__main__':
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-7 00:00:32 | 显示全部楼层    本楼为最佳答案   
终于帮你改完了 满意请给最佳
2021-04-06_23-59-55 by Sharpstar_kylin.png
2021-04-07_00-00-05 by Sharpstar_kylin.png
import requests
import bs4
import openpyxl

import os
# os.chdir("D:\BianCheng\Pythonwork\爬虫爬出来资料")

header ={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}

def open_url(url):
    res = requests.get(url, headers=header)
    return res


def look_movie(res):
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    targets = soup.find_all("div", class_='hd')
    list1 = []
    for each in targets:
        list1.append(each.a.span.text)
    return list1


def main():
    yeshu = 0

    data = []
    while yeshu <= 225:
        url = "http://movie.douban.com/top250?start=" + str(yeshu) + "&filter="
        res = open_url(url)
        data.extend(look_movie(res))
        yeshu += 25
    to_excel(data, yeshu)


def to_excel(data, yeshu):
    wb = openpyxl.Workbook()
    wb.guess_types = True
    ws = wb.active
    data_rank = [str(i) for i in range(yeshu+1-len(data), yeshu+1)]
    ws.append(data_rank)
    for movie_rank,movie_name in zip(data_rank, data):
        print("%s 排名:%s" % (movie_name, movie_rank))
    print(data)
    ws.append(data)
    wb.save("豆瓣250电影榜.xlsx")


if __name__ == '__main__':
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-4-7 01:08:54 | 显示全部楼层
591821661 发表于 2021-4-7 00:00
终于帮你改完了 满意请给最佳

多谢大佬解惑
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-7 10:10:36 | 显示全部楼层
根据大佬的代码,自己弄出了自己想要的部分模样
import requests
import bs4
import openpyxl

#import os
# os.chdir("D:\BianCheng\Pythonwork\爬虫爬出来资料")

header ={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}

def open_url(url):
    res = requests.get(url, headers=header)
    return res


def look_movie(res):
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    targets = soup.find_all("div", class_='hd')
    data = []
    for each in targets:
        data.append(each.a.span.text)
    return data


def main():
    yeshu = 0

    data = []
    while yeshu <= 225:
        url = "http://movie.douban.com/top250?start=" + str(yeshu) + "&filter="
        res = open_url(url)
        data.extend(look_movie(res))
        yeshu += 25
    to_excel(data, yeshu)


def to_excel(data, yeshu):
    wb = openpyxl.Workbook()
    wb.guess_types = True
    ws = wb.active
    #表格三件套

    # data_rank = [str(i) for i in range(yeshu+1-len(data), yeshu+1)]

    # for movie_rank,movie_name in zip(data_rank, data):
    #     print("%s 排名:%s" % (movie_name, movie_rank))
    nub = 0
    data_rank = []
    kongge = []
    for i in range(1,251):
        data_rank.append("第%d名"%i)
        if i % 10 == 0:
            ws.append(data_rank)
            ws.append(data[0+nub:i])
            ws.append(kongge)
            nub += 10
            data_rank = []

    wb.save("豆瓣250电影榜.xlsx")


if __name__ == '__main__':
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-22 16:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表