鱼C论坛

 找回密码
 立即注册
查看: 818|回复: 4

[已解决]新手小白学习爬虫,请大佬帮忙解答一下,万分感谢~~

[复制链接]
发表于 2023-4-21 17:23:41 | 显示全部楼层 |阅读模式

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

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

x
我想爬取这个页面的所有电影名称

代码如下:
import requests
from bs4 import BeautifulSoup
import openpyxl

def open_url(url):
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
    }
    html = requests.get(url, headers=headers).text
    return html

def get_data(html):
    content = BeautifulSoup(html, "html.parser")
    data = content.find_all('li', attrs={'class': 'stitle'})
    cinema_list = []
    for d in data:
        plist = d.find('a')['title']
        cinema_list.append(plist)
        return cinema_list

def save_as_excel(mylist):
    wb = openpyxl.Workbook()
    ws = wb.active

    ws['A1'] = '电影'

    for d in mylist:
        ws.append([d])

    wb.save('2023电影排行.xlsx')

def main():
    url = 'https://movie.douban.com/cinema/nowplaying/shanghai/'
    html = open_url(url)
    mylist = get_data(html)
    save_as_excel(mylist)

if __name__ == '__main__':
    main()


显示结果只有一个灌篮高手,请问那是为何呢?求大佬帮忙解答一下,谢谢
最佳答案
2023-4-21 17:27:48


你的代码中出现了一个小问题。在 get_data 函数中的 for 循环后,需要将 `return cinema_list` 移出循环之外。这是因为当前的代码只会循环到第一个电影名称,然后就直接返回了。

修改后的代码如下所示:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. import openpyxl

  4. def open_url(url):
  5.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
  6.     html = requests.get(url, headers=headers).text
  7.     return html

  8. def get_data(html):
  9.     content = BeautifulSoup(html, "html.parser")
  10.     data = content.find_all('li', attrs={'class': 'stitle'})
  11.     cinema_list = []
  12.     for d in data:
  13.         plist = d.find('a')['title']
  14.         cinema_list.append(plist)
  15.     return cinema_list

  16. def save_as_excel(mylist):
  17.     wb = openpyxl.Workbook()
  18.     ws = wb.active

  19.     ws['A1'] = '电影'

  20.     for d in mylist:
  21.         ws.append([d])

  22.     wb.save('2023电影排行.xlsx')

  23. def main():
  24.     url = 'https://movie.douban.com/cinema/nowplaying/shanghai/'
  25.     html = open_url(url)
  26.     mylist = get_data(html)
  27.     save_as_excel(mylist)

  28. if __name__ == '__main__':
  29.     main()
复制代码


运行以上代码后,你应该会得到一个电影名称的列表,包含当前正在上映的所有电影名称。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-4-21 17:27:03 | 显示全部楼层
本帖最后由 歌者文明清理员 于 2023-4-21 17:29 编辑

tempzzzz15.PNG
return会直接结束函数,所以缩进要往前移
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-21 17:27:48 | 显示全部楼层    本楼为最佳答案   


你的代码中出现了一个小问题。在 get_data 函数中的 for 循环后,需要将 `return cinema_list` 移出循环之外。这是因为当前的代码只会循环到第一个电影名称,然后就直接返回了。

修改后的代码如下所示:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. import openpyxl

  4. def open_url(url):
  5.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
  6.     html = requests.get(url, headers=headers).text
  7.     return html

  8. def get_data(html):
  9.     content = BeautifulSoup(html, "html.parser")
  10.     data = content.find_all('li', attrs={'class': 'stitle'})
  11.     cinema_list = []
  12.     for d in data:
  13.         plist = d.find('a')['title']
  14.         cinema_list.append(plist)
  15.     return cinema_list

  16. def save_as_excel(mylist):
  17.     wb = openpyxl.Workbook()
  18.     ws = wb.active

  19.     ws['A1'] = '电影'

  20.     for d in mylist:
  21.         ws.append([d])

  22.     wb.save('2023电影排行.xlsx')

  23. def main():
  24.     url = 'https://movie.douban.com/cinema/nowplaying/shanghai/'
  25.     html = open_url(url)
  26.     mylist = get_data(html)
  27.     save_as_excel(mylist)

  28. if __name__ == '__main__':
  29.     main()
复制代码


运行以上代码后,你应该会得到一个电影名称的列表,包含当前正在上映的所有电影名称。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-21 17:37:16 | 显示全部楼层
isdkz 发表于 2023-4-21 17:27
你的代码中出现了一个小问题。在 get_data 函数中的 for 循环后,需要将 `return cinema_list` 移出循 ...

谢谢大佬,可以了~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-21 17:38:10 | 显示全部楼层
liubulong 发表于 2023-4-21 17:37
谢谢大佬,可以了~~

不客气
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 02:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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