鱼C论坛

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

[已解决]没有生成Excel文件

[复制链接]
发表于 2021-6-6 15:37:09 | 显示全部楼层 |阅读模式
5鱼币
使用Python读写Excel文件(1)课程,按照视频把代码改了,没有生成Excel文件!


  1. import requests
  2. import bs4
  3. import re
  4. import openpyxl

  5. def open_url(url):
  6.     # 使用代理
  7.     # proxies = {"http": "127.0.0.1:1080", "https": "127.0.0.1:1080"}
  8.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}

  9.     # res = requests.get(url, headers=headers, proxies=proxies)
  10.     res = requests.get(url, headers=headers)

  11.     return res

  12. def find_movies(res):
  13.     soup = bs4.BeautifulSoup(res.text, 'html.parser')

  14.     # 电影名
  15.     movies = []
  16.     targets = soup.find_all("div", class_="hd")
  17.     for each in targets:
  18.         movies.append(each.a.span.text)

  19.     # 评分
  20.     ranks = []
  21.     targets = soup.find_all("span", class_="rating_num")
  22.     for each in targets:
  23.         ranks.append(each.text)

  24.     # 资料
  25.     messages = []
  26.     targets = soup.find_all("div", class_="bd")
  27.     for each in targets:
  28.         try:
  29.             messages.append(each.p.text.split('\n')[1].strip() + each.p.text.split('\n')[2].strip())
  30.         except:
  31.             continue

  32.     result = []
  33.     length = len(movies)
  34.     for i in range(length):
  35.         result.append([movies[i],ranks[i],messages[i]])

  36.     return result

  37. # 找出一共有多少个页面
  38. def find_depth(res):
  39.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  40.     depth = soup.find('span', class_='next').previous_sibling.previous_sibling.text

  41.     return int(depth)

  42. def main():
  43.     host = "https://movie.douban.com/top250"
  44.     res = open_url(host)
  45.     depth = find_depth(res)

  46.     result = []
  47.     for i in range(depth):
  48.         url = host + '/?start=' + str(25 * i)
  49.         res = open_url(url)
  50.         result.extend(find_movies(res))

  51. def save_to_excel(result):
  52.     wb = openpyxl.Workbook()
  53.     ws = wb.active

  54.     ws['A1'] = "电影名称"
  55.     ws['B1'] = "评分"
  56.     ws['C1'] = "资料"

  57.     for each in result:
  58.         ws.append(each)

  59.     wb.save("豆瓣top250电影。xlsx")
  60.    
  61. if __name__ == "__main__":
  62.     main()
  63.    
复制代码
最佳答案
2021-6-6 15:37:10
本帖最后由 Twilight6 于 2021-6-6 15:48 编辑



你的 main 函数忘记调用 save_to_excel(result) 了,在 main 的函数最后加上 save_to_excel(result)

你 wb.save("豆瓣top250电影xlsx") 写成 中文的 句号了... 改成英文的

参考代码:

  1. import requests
  2. import bs4
  3. import re
  4. import openpyxl


  5. def open_url(url):
  6.     # 使用代理
  7.     # proxies = {"http": "127.0.0.1:1080", "https": "127.0.0.1:1080"}
  8.     headers = {
  9.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}

  10.     # res = requests.get(url, headers=headers, proxies=proxies)
  11.     res = requests.get(url, headers=headers)

  12.     return res


  13. def find_movies(res):
  14.     soup = bs4.BeautifulSoup(res.text, 'html.parser')

  15.     # 电影名
  16.     movies = []
  17.     targets = soup.find_all("div", class_="hd")
  18.     for each in targets:
  19.         movies.append(each.a.span.text)

  20.     # 评分
  21.     ranks = []
  22.     targets = soup.find_all("span", class_="rating_num")
  23.     for each in targets:
  24.         ranks.append(each.text)

  25.     # 资料
  26.     messages = []
  27.     targets = soup.find_all("div", class_="bd")
  28.     for each in targets:
  29.         try:
  30.             messages.append(each.p.text.split('\n')[1].strip() + each.p.text.split('\n')[2].strip())
  31.         except:
  32.             continue

  33.     result = []
  34.     length = len(movies)
  35.     for i in range(length):
  36.         result.append([movies[i], ranks[i], messages[i]])

  37.     return result


  38. # 找出一共有多少个页面
  39. def find_depth(res):
  40.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  41.     depth = soup.find('span', class_='next').previous_sibling.previous_sibling.text

  42.     return int(depth)


  43. def main():
  44.     host = "https://movie.douban.com/top250"
  45.     res = open_url(host)
  46.     depth = find_depth(res)

  47.     result = []
  48.     for i in range(depth):
  49.         url = host + '/?start=' + str(25 * i)
  50.         res = open_url(url)
  51.         result.extend(find_movies(res))
  52.     save_to_excel(result)


  53. def save_to_excel(result):
  54.     wb = openpyxl.Workbook()
  55.     ws = wb.active

  56.     ws['A1'] = "电影名称"
  57.     ws['B1'] = "评分"
  58.     ws['C1'] = "资料"

  59.     for each in result:
  60.         ws.append(each)

  61.     wb.save("豆瓣top250电影.xlsx")


  62. if __name__ == "__main__":
  63.     main()

复制代码


最佳答案

查看完整内容

你的 main 函数忘记调用 save_to_excel(result) 了,在 main 的函数最后加上 save_to_excel(result) 你 wb.save("豆瓣top250电影。xlsx") 写成 中文的 句号了... 改成英文的 参考代码:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-6 15:37:10 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2021-6-6 15:48 编辑



你的 main 函数忘记调用 save_to_excel(result) 了,在 main 的函数最后加上 save_to_excel(result)

你 wb.save("豆瓣top250电影xlsx") 写成 中文的 句号了... 改成英文的

参考代码:

  1. import requests
  2. import bs4
  3. import re
  4. import openpyxl


  5. def open_url(url):
  6.     # 使用代理
  7.     # proxies = {"http": "127.0.0.1:1080", "https": "127.0.0.1:1080"}
  8.     headers = {
  9.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}

  10.     # res = requests.get(url, headers=headers, proxies=proxies)
  11.     res = requests.get(url, headers=headers)

  12.     return res


  13. def find_movies(res):
  14.     soup = bs4.BeautifulSoup(res.text, 'html.parser')

  15.     # 电影名
  16.     movies = []
  17.     targets = soup.find_all("div", class_="hd")
  18.     for each in targets:
  19.         movies.append(each.a.span.text)

  20.     # 评分
  21.     ranks = []
  22.     targets = soup.find_all("span", class_="rating_num")
  23.     for each in targets:
  24.         ranks.append(each.text)

  25.     # 资料
  26.     messages = []
  27.     targets = soup.find_all("div", class_="bd")
  28.     for each in targets:
  29.         try:
  30.             messages.append(each.p.text.split('\n')[1].strip() + each.p.text.split('\n')[2].strip())
  31.         except:
  32.             continue

  33.     result = []
  34.     length = len(movies)
  35.     for i in range(length):
  36.         result.append([movies[i], ranks[i], messages[i]])

  37.     return result


  38. # 找出一共有多少个页面
  39. def find_depth(res):
  40.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  41.     depth = soup.find('span', class_='next').previous_sibling.previous_sibling.text

  42.     return int(depth)


  43. def main():
  44.     host = "https://movie.douban.com/top250"
  45.     res = open_url(host)
  46.     depth = find_depth(res)

  47.     result = []
  48.     for i in range(depth):
  49.         url = host + '/?start=' + str(25 * i)
  50.         res = open_url(url)
  51.         result.extend(find_movies(res))
  52.     save_to_excel(result)


  53. def save_to_excel(result):
  54.     wb = openpyxl.Workbook()
  55.     ws = wb.active

  56.     ws['A1'] = "电影名称"
  57.     ws['B1'] = "评分"
  58.     ws['C1'] = "资料"

  59.     for each in result:
  60.         ws.append(each)

  61.     wb.save("豆瓣top250电影.xlsx")


  62. if __name__ == "__main__":
  63.     main()

复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-6 15:49:22 | 显示全部楼层
本帖最后由 FengHL 于 2021-6-6 15:50 编辑
Twilight6 发表于 2021-6-6 15:46
你的 main 函数忘记调用 save_to_excel(result) 了,在 main 的函数最后加上 save_to_excel(result)

...


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-6 15:50:05 | 显示全部楼层
FengHL 发表于 2021-6-6 15:49
改了,好像还是不行



main 函数加了 save_to_excel(result) 了嘛?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-6 15:53:13 | 显示全部楼层
Twilight6 发表于 2021-6-6 15:50
main 函数加了 save_to_excel(result) 了嘛?

可以了,万分感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-11 23:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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