鱼C论坛

 找回密码
 立即注册
查看: 1394|回复: 23

[已解决]妹子是找到, 但是怎么搂在怀里呀!!!!

[复制链接]
发表于 2020-6-13 12:58:22 | 显示全部楼层 |阅读模式

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

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

x
如图,我已得到该图片了, 但是怎么保存下来。。。

with open (photodata + '.jpg' , 'wb') as f:
TypeError: can only concatenate list (not "str") to list
最佳答案
2020-6-13 14:51:29
maxliu06 发表于 2020-6-13 14:41
意思 就是再请求一下 找到的图片url  ??~!!!

当然要,你爬到链接就单纯的是个链接  而且这个网站有反爬 你要在请求头要这么写:
  1. headers = {
  2.         'Referer': 'https://www.mzitu.com/',
  3.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  4.                          }
复制代码


1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-13 13:03:34 | 显示全部楼层
TypeError: can only concatenate list (not "str") to list


看你报错 应该是 前面代码是  .text 的  你改成 .content就好

以防万一 还是发个代码看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 13:09:40 | 显示全部楼层
没代码啊……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 13:12:36 | 显示全部楼层
Twilight6 发表于 2020-6-13 13:03
看你报错 应该是 前面代码是  .text 的  你改成 .content就好

以防万一 还是发个代码看看
  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. import requests
  4. import re
  5. import os


  6. def photolists(html):
  7.     """获取每一页的图集url 及 标题 """
  8.     photo_urls = re.findall(r'<li><a href="(.*?)" target="_blank"><img', html)   # 图片url
  9.     photo_titles = re.findall(r'alt=\'(.*?)\' width=\'236\' height=\'354\' />', html)  # 图集标题
  10.     return list(zip(photo_titles, photo_urls))

  11. def photourl(url,headers):
  12.     response = requests.get(url=url, headers=headers)
  13.     html = response.text
  14.     # 图集最大页码
  15.     maxpage = re.findall(r'>…</span><a href=\'.*?\'><span>(\d+)</span></a><a href=\'.*?\'><span>下一页', html)[0]
  16.     pages = range(1,int(maxpage)+1)
  17.     for page in pages:
  18.         # print(url+'/'+str(page)) # 图集的url
  19.         phtoturl = url+'/'+str(page)  # 每张图片的真正url
  20.         response = requests.get(url=phtoturl, headers=headers)
  21.         html = response.text
  22.         photodata = re.findall(r'<img class="blur" src="(.*?)" alt=',html)
  23.         phototname = photodata[0].split(".")[-2]
  24.         with open (photodata + '.jpg' , 'wb') as f:
  25.             f.write(photodata[0].content)

  26.         break




  27. # 主程序
  28. def main():
  29.     # 主页url
  30.     url = 'https://www.mzitu.com/'
  31.     #构造请求头
  32.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  33.                          }
  34.     response = requests.get(url, headers=headers)
  35.     photos = photolists(response.text)  # 得到当前页的所有图集 url及标题
  36.     for i in range(len(photos)): # 打开每一个图集
  37.         #print(photos[i][1])
  38.         # 创建文件夹
  39.         if not os.path.exists(r'D:\测试\{}'.format(photos[i][0])):
  40.             os.mkdir(r'D:\测试\{}'.format(photos[i][0]))
  41.         # 获取图集 并保存
  42.         photourl(photos[i][1], headers)

  43.         break



  44. #程序入口
  45. if __name__ == '__main__':
  46.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 13:13:36 | 显示全部楼层
Twilight6 发表于 2020-6-13 13:03
看你报错 应该是 前面代码是  .text 的  你改成 .content就好

以防万一 还是发个代码看看

看你写的那个,  运行后 找不到妹子呀。。
所以今天想自己来找找
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 13:16:10 | 显示全部楼层
maxliu06 发表于 2020-6-13 13:13
看你写的那个,  运行后 找不到妹子呀。。
所以今天想自己来找找

photos = photolists(response.text)

这边改成

photos = photolists(response.content)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 13:19:01 | 显示全部楼层
Twilight6 发表于 2020-6-13 13:16
photos = photolists(response.text)

这边改成

这是啥意思。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 13:21:53 | 显示全部楼层
maxliu06 发表于 2020-6-13 13:19
这是啥意思。。。

text 返回的是unicode 型的数据

content返回的是bytes,二进制型的数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 13:24:32 | 显示全部楼层
maxliu06 发表于 2020-6-13 13:19
这是啥意思。。。

等等  是我看错了   我重新看下代码  不是这里问题好像
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 13:43:44 | 显示全部楼层
maxliu06 发表于 2020-6-13 13:19
这是啥意思。。。

要改代码了  网站源码变了,我的也爬不到了  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 14:02:39 | 显示全部楼层
Twilight6 发表于 2020-6-13 13:43
要改代码了  网站源码变了,我的也爬不到了

但是我能得到图片的url呀。。  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 14:03:11 | 显示全部楼层
Twilight6 发表于 2020-6-13 13:43
要改代码了  网站源码变了,我的也爬不到了

得到 这东西  ,,不就可以保存下来的吗
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 14:24:02 | 显示全部楼层
maxliu06 发表于 2020-6-13 14:03
得到 这东西  ,,不就可以保存下来的吗

  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. import requests
  4. import re
  5. import os


  6. def photolists(html):
  7.     """获取每一页的图集url 及 标题 """
  8.     photo_urls = re.findall(r'<li><a href="(.*?)" target="_blank"><img', html)   # 图片url
  9.     photo_titles = re.findall(r'alt=\'(.*?)\' width=\'236\' height=\'354\' />', html)  # 图集标题
  10.     return list(zip(photo_titles, photo_urls))

  11. def photourl(url,headers):
  12.     response = requests.get(url=url, headers=headers)
  13.     html = response.text
  14.     # 图集最大页码
  15.     maxpage = re.findall(r'>…</span><a href=\'.*?\'><span>(\d+)</span></a><a href=\'.*?\'><span>下一页', html)[0]
  16.     pages = range(1,int(maxpage)+1)
  17.     for page in pages:
  18.         # print(url+'/'+str(page)) # 图集的url
  19.         phtoturl = url+'/'+str(page)  # 每张图片的真正url
  20.         response = requests.get(url=phtoturl, headers=headers)
  21.         html = response.text
  22.         photodata = re.findall(r'<img class="blur" src="(.*?)" alt=',html)
  23.         phototname = photodata[0].split(".")[-2].split('/')[-1]
  24.         photo = requests.get(url=photodata[0], headers=headers)
  25.         with open (phototname + '.jpg' , 'wb') as f:
  26.             print('123')
  27.             f.write(photo.content)

  28.         break




  29. # 主程序
  30. def main():
  31.     # 主页url
  32.     url = 'https://www.mzitu.com/'
  33.     #构造请求头
  34.     headers = {
  35.         'Referer': 'https://www.mzitu.com/',
  36.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  37.                          }
  38.     response = requests.get(url, headers=headers)
  39.     photos = photolists(response.text)  # 得到当前页的所有图集 url及标题
  40.     for i in range(len(photos)): # 打开每一个图集
  41.         #print(photos[i][1])
  42.         # 创建文件夹
  43.         if not os.path.exists(r'{}'.format(photos[i][0])):
  44.             os.mkdir(r'{}'.format(photos[i][0]))
  45.         # 获取图集 并保存
  46.         photourl(photos[i][1], headers)

  47.         break



  48. #程序入口
  49. if __name__ == '__main__':
  50.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 14:41:12 | 显示全部楼层

意思 就是再请求一下 找到的图片url  ??~!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 14:51:29 | 显示全部楼层    本楼为最佳答案   
maxliu06 发表于 2020-6-13 14:41
意思 就是再请求一下 找到的图片url  ??~!!!

当然要,你爬到链接就单纯的是个链接  而且这个网站有反爬 你要在请求头要这么写:
  1. headers = {
  2.         'Referer': 'https://www.mzitu.com/',
  3.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  4.                          }
复制代码


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

使用道具 举报

 楼主| 发表于 2020-6-13 15:31:38 | 显示全部楼层
Twilight6 发表于 2020-6-13 14:51
当然要,你爬到链接就单纯的是个链接  而且这个网站有反爬 你要在请求头要这么写:

  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. import requests
  4. import re
  5. import os

  6. def photolists(html):
  7.     """获取每一页的图集url 及 标题 """
  8.     photo_urls = re.findall(r'<li><a href="(.*?)" target="_blank"><img', html)   # 图片url
  9.     photo_titles = re.findall(r'alt=\'(.*?)\' width=\'236\' height=\'354\' />', html)  # 图集标题
  10.     return list(zip(photo_titles, photo_urls))

  11. def photourl(url, headers):
  12.     response = requests.get(url=url, headers=headers)
  13.     html = response.text
  14.     # 图集最大页码
  15.     maxpage = re.findall(r'>…</span><a href=\'.*?\'><span>(\d+)</span></a><a href=\'.*?\'><span>下一页', html)
  16.     #print(maxpage)
  17.     pages = range(1,int(maxpage[0])+1)  # 每个图集的最大页码
  18.     photo =[]
  19.     for page in pages:
  20.         # print(url+'/'+str(page)) # 图集的url
  21.         try:
  22.             phtoturl = url+'/'+str(page)  # 每张图片的真正url
  23.             response = requests.get(url=phtoturl, headers=headers)
  24.             html = response.text
  25.             photodata = re.findall(r'<img class="blur" src="(.*?)" alt=',html)
  26.             phototname = photodata[0].split(".")[-2].split('/')[-1]
  27.             photo.append([photodata[0],phototname])
  28.         except:
  29.             break

  30.     return photo

  31. # 主程序
  32. def main():
  33.     # 主页url
  34.     url = 'https://www.mzitu.com/'
  35.     # 构造请求头
  36.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  37.                          }
  38.     response = requests.get(url, headers=headers)
  39.     photos = photolists(response.text)  # 得到当前页的所有图集 url及标题
  40.     for i in range(len(photos)):  # 打开每一个图集
  41.         # print(photos[i][1])
  42.         # 创建文件夹
  43.         #if not os.path.exists(r'D:\{}'.format(photos[i][0])):
  44.         #    os.mkdir(r'D:\{}'.format(photos[i][0]))
  45.         # 获取图集 并保存
  46.         phdatas = photourl(photos[i][1], headers) # 图集中每个图片的url
  47.         # 下载图片
  48.         for photo in phdatas:
  49.             print(photo[0])
  50.             print(photo[1])
  51.             res = requests.get(url=photo[0], headers=headers)
  52.             with open(photo[1] + '.jpg', 'wb') as f:
  53.                 f.write(res.content)
  54.             break
  55.         break

  56. #程序入口
  57. if __name__ == '__main__':
  58.     main()
复制代码

#程序入口
if __name__ == '__main__':
    main()
[/code]


大侠,再请教一下。。 为什么我保存的图片,显示不了。用你的却可以的那个就可以显示。 这问题出在哪里?
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 15:37:16 | 显示全部楼层
maxliu06 发表于 2020-6-13 15:31
#程序入口
if __name__ == '__main__':
    main()

你看清楚我上面一楼的评论,怎么都不认真看回复捏?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 15:43:19 | 显示全部楼层
Twilight6 发表于 2020-6-13 15:37
你看清楚我上面一楼的评论,怎么都不认真看回复捏?
  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. import requests
  4. import re
  5. import os

  6. def photolists(html):
  7.     """获取每一页的图集url 及 标题 """
  8.     photo_urls = re.findall(r'<li><a href="(.*?)" target="_blank"><img', html)   # 图片url
  9.     photo_titles = re.findall(r'alt=\'(.*?)\' width=\'236\' height=\'354\' />', html)  # 图集标题
  10.     return list(zip(photo_titles, photo_urls))

  11. def photourl(url, headers):
  12.     response = requests.get(url=url, headers=headers)
  13.     html = response.text
  14.     # 图集最大页码
  15.     maxpage = re.findall(r'>…</span><a href=\'.*?\'><span>(\d+)</span></a><a href=\'.*?\'><span>下一页', html)
  16.     #print(maxpage)
  17.     pages = range(1,int(maxpage[0])+1)  # 每个图集的最大页码
  18.     photo =[]
  19.     for page in pages:
  20.         # print(url+'/'+str(page)) # 图集的url
  21.         try:
  22.             phtoturl = url+'/'+str(page)  # 每张图片的真正url
  23.             response = requests.get(url=phtoturl, headers=headers)
  24.             html = response.text
  25.             photodata = re.findall(r'<img class="blur" src="(.*?)" alt=',html)
  26.             phototname = photodata[0].split(".")[-2].split('/')[-1]
  27.             photo.append([photodata[0],phototname])
  28.         except:
  29.             break

  30.     return photo

  31. # 主程序
  32. def main():
  33.     # 主页url
  34.     url = 'https://www.mzitu.com/'
  35.     # 构造请求头
  36.     headers = {
  37.         'Referer': 'https://www.mzitu.com/',
  38.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
  39.     }

  40.     response = requests.get(url, headers=headers)
  41.     photos = photolists(response.text)  # 得到当前页的所有图集 url及标题
  42.     for i in range(len(photos)):  # 打开每一个图集
  43.         # print(photos[i][1])
  44.         # 创建文件夹
  45.         #if not os.path.exists(r'D:\{}'.format(photos[i][0])):
  46.         #    os.mkdir(r'D:\{}'.format(photos[i][0]))
  47.         # 获取图集 并保存
  48.         phdatas = photourl(photos[i][1], headers) # 图集中每个图片的url
  49.         # 下载图片
  50.         for photo in phdatas:
  51.             print(photo[0])
  52.             print(photo[1])
  53.             res = requests.get(url=photo[0], headers=headers)
  54.             with open(photo[1] + '.jpg', 'wb') as f:
  55.                 f.write(res.content)
  56.             break
  57.         break

  58. #程序入口
  59. if __name__ == '__main__':
  60.     main()
复制代码



  没注意到那个信息。。。   我换了你说的请求头了。    下载回来的图片 , 说是已损坏的。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-13 15:44:31 | 显示全部楼层
maxliu06 发表于 2020-6-13 15:43
没注意到那个信息。。。   我换了你说的请求头了。    下载回来的图片 , 说是已损坏的 ...

你试试我的代码 就会发现可以运行了 我现在在外面不方便嘿嘿
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-13 15:45:02 | 显示全部楼层
Twilight6 发表于 2020-6-13 15:44
你试试我的代码 就会发现可以运行了 我现在在外面不方便嘿嘿

你的确实可以。。。   

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
Twilight6 + 5 + 5 鱼C有你更精彩^_^

查看全部评分

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 18:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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