鱼C论坛

 找回密码
 立即注册
查看: 2651|回复: 10

括号里面'号错误

[复制链接]
发表于 2020-9-14 12:03:06 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 过默 于 2020-9-14 12:20 编辑
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import time


  4. headers = {
  5.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
  6. }


  7. with open('豆瓣电影排行榜.txt', 'w', encoding='utf-8') as file:
  8.     for page in range(10):
  9.         url = 'https://movie.douban.com/top250?start={}&filter='.format(page*25)
  10.         res = requests.get(url, headers=headers)
  11.         soup = BeautifulSoup(res.text, 'html.parser')
  12.         for item in soup.select('div.item'):
  13.             tag = item.select('div.hd a')
  14.             link = tag[0]['href']  # 地址
  15.             name = tag[0].select('span.title')[0].text  # 影片名称
  16.             content = item.select('div.bd p.quote span.inq')  # 简介
  17.             score = item.select('div.bd div.star span.rating_num')[0].text  # 评分
  18.             number_of_score = item.select('div.bd div.star')[0].text.split()[-1]  # 评价人数
  19.             information = item.select('div.bd p')[0].text.split()  # 信息列表
  20.             if [y for y in information if y.isdigit()]:
  21.                 year = [y for y in information if y.isdigit()][0] + '年'  # 上映时间
  22.             else:
  23.                 year = '暂无数据'
  24.             if content:  # 判断是否为None
  25.                 content = content[0].text
  26.             else:
  27.                 content = '此影片尚无简介'

  28.             file.write(f'影片名称:《{name}》\n')
  29.             file.write(f'上映时间:{year}\n')
  30.             file.write(f'影片评分:{number_of_score},得分{score}分\n')
  31.             file.write(f'影片简介:{content}\n')
  32.             file.write(f'影片地址:{link}\n')
  33.             file.write('\n')

  34.         print(f'第{page+1}页爬取完毕')
  35.         time.sleep(2)
  36.     else:
  37.         print('done!')
复制代码


复制粘贴,怎么打印不出来,‘’错误,怎么回事? file.write(f'影片名称:《{name}》\n') 括号里面'号错误
QQ图片20200914121908.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-14 12:08:17 | 显示全部楼层
用你的代码运行了一遍,很正常啊,能保存到txt文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-14 12:16:38 | 显示全部楼层
疾风怪盗 发表于 2020-9-14 12:08
用你的代码运行了一遍,很正常啊,能保存到txt文件

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}

影片名称:《{name}》
上映时间:{year}
影片评分:{number_of_score},得分{score}分
影片简介:{content}
影片地址:{link}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 12:18:53 | 显示全部楼层
我运行保存下来是这样的:

影片名称:《肖申克的救赎》
上映时间:1994年
影片评分:2141294人评价,得分9.7分
影片简介:希望让人自由。
影片地址:https://movie.douban.com/subject/1292052/

影片名称:《霸王别姬》
上映时间:1993年
影片评分:1586422人评价,得分9.6分
影片简介:风华绝代。
影片地址:https://movie.douban.com/subject/1291546/
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-14 12:20:51 | 显示全部楼层
疾风怪盗 发表于 2020-9-14 12:18
我运行保存下来是这样的:

影片名称:《肖申克的救赎》

哪里坏了,哪里错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 12:21:27 | 显示全部楼层
本帖最后由 疾风怪盗 于 2020-9-14 12:23 编辑
过默 发表于 2020-9-14 12:20
哪里坏了,哪里错了


就是用你贴的代码,什么都没改,就运行,保存,很正常

你的这个确保有f吧?
  1. print(f'第{page+1}页爬取完毕')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 12:26:31 From FishC Mobile | 显示全部楼层
本帖最后由 hrp 于 2020-9-14 12:39 编辑
过默 发表于 2020-9-14 12:20
哪里坏了,哪里错了


少年,f-string 字符串格式化 Python 3.6 开始支持,你的 Python 版本是不是 3.6 以下的?我看你用的是 xp 系统,十有八九是 3.5 或者以下的 Python 版本了。
要么改成 str.format 或者 % 格式化方式,要么换系统,装更高版本的 Python 环境。

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


  4. headers = {
  5.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
  6. }


  7. with open('豆瓣电影排行榜.txt', 'w', encoding='utf-8') as file:
  8.     for page in range(10):
  9.         url = 'https://movie.douban.com/top250?start={}&filter='.format(page*25)
  10.         res = requests.get(url, headers=headers)
  11.         soup = BeautifulSoup(res.text, 'html.parser')
  12.         for item in soup.select('div.item'):
  13.             tag = item.select('div.hd a')
  14.             link = tag[0]['href']  # 地址
  15.             name = tag[0].select('span.title')[0].text  # 影片名称
  16.             content = item.select('div.bd p.quote span.inq')  # 简介
  17.             score = item.select('div.bd div.star span.rating_num')[0].text  # 评分
  18.             number_of_score = item.select('div.bd div.star')[0].text.split()[-1]  # 评价人数
  19.             information = item.select('div.bd p')[0].text.split()  # 信息列表
  20.             if [y for y in information if y.isdigit()]:
  21.                 year = [y for y in information if y.isdigit()][0] + '年'  # 上映时间
  22.             else:
  23.                 year = '暂无数据'
  24.             if content:  # 判断是否为None
  25.                 content = content[0].text
  26.             else:
  27.                 content = '此影片尚无简介'

  28.             file.write('影片名称:《%s》\n' % name)
  29.             file.write('上映时间:%s\n' % year)
  30.             file.write('影片评分:%s,得分%s分\n' % (number_of_score, score))
  31.             file.write('影片简介:%s\n' % content)
  32.             file.write('影片地址:%s\n' % link)
  33.             file.write('\n')

  34.         print('第%d页爬取完毕' % (page + 1))
  35.         time.sleep(2)
  36.     else:
  37.         print('done!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-14 12:46:13 | 显示全部楼层
hrp 发表于 2020-9-14 12:26
少年,f-string 字符串格式化 Python 3.6 开始支持,你的 Python 版本是不是 3.6 以下的?我看你用的是 ...

  1. import re,requests,os
  2. from lxml import etree

  3. def download_pics (url):
  4.     response = requests.get (url,headers = headers1)
  5.     base_link = etree.HTML (response.text).xpath ('//div[@class="main-image"]//img/@src')[0][0:-6] #提取首张图片的下载地址
  6.     page_info = etree.HTML (response.text).xpath ('//div[@class="pagenavi"]//span/text()')    #提取当前系列图片的页码信息列表
  7.     max_page = int(page_info[-2])   #提取图片的最大页码
  8.     for i in range(1,max_page+1):
  9.         page= str (i).zfill(2)
  10.         download_link = base_link+ str(page)+'.jpg'
  11.         headers = {'Referer':url+'{}'.format (i),'User-Agent': 'Mozilla/5.0(Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
  12.         response = requests.get (download_link,headers = headers)
  13.         pic_name = download_link[-9:-4]
  14.         print ('目前第{}套妹子,共{}张  正在下载第{} 张图片......'.format (list_cnt,max_page,i))
  15.         with open (save_path +'\\'+pic_name+'.jpg','wb') as f:
  16.             f.write (response.content)

  17. def get_list (url):  
  18.     global list_cnt,save_path
  19.     list_cnt = 1
  20.     reponse = requests.get (url,headers = headers1)
  21.     regax = '\d\d\w\:.+\>'
  22.     list_pool = re.findall (regax,reponse.text)
  23.     for li in list_pool:
  24.         url = li.split(' ')[2].split('"')[1]
  25.         save_path = '美女套图\\EP'+str(list_cnt).zfill(4)
  26.         if not os.path.exists(save_path):   #新建一个存放图片的目录
  27.             os.mkdir(save_path)
  28.         download_pics (url)
  29.         list_cnt += 1
  30.         
  31. def main ():
  32.     if not os.path.exists('美女套图'):   #新建一个存放图片的目录
  33.         os.mkdir('美女套图')
  34.     input ('本程序将采集全网套图,采集时间会因站点图片增加而延长,确定采集请按回车键')
  35.     os.startfile ('美女套图')
  36.     get_list (url)

  37. if __name__ == '__main__':
  38.     url = 'https://www.mzitu.com/all/'
  39.     headers1 = {'user-agent': 'Mozilla/5.0'}
  40.     main ()
复制代码

这个怎么也爬不出来,没显示图片
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 14:08:58 From FishC Mobile | 显示全部楼层
过默 发表于 2020-9-14 12:46
这个怎么也爬不出来,没显示图片

我用手机不方便调试,等大佬们帮你吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 15:08:12 | 显示全部楼层
过默 发表于 2020-9-14 12:46
这个怎么也爬不出来,没显示图片

你这个链接404 你用其他链接 这个站有一定的防爬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-14 18:50:58 From FishC Mobile | 显示全部楼层
过默 发表于 2020-9-14 12:20
哪里坏了,哪里错了

你应该没有 f-string 这个用法
要不要升级系统,换新版本
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 05:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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