鱼C论坛

 找回密码
 立即注册
查看: 573|回复: 1

[已解决]求助列表历遍命令

[复制链接]
发表于 2018-12-31 15:56:40 | 显示全部楼层 |阅读模式

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

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

x
先上代码

  1. import urllib.request
  2. import requests
  3. import re
  4. import os
  5. #####get_pagelist、get_postlist、get_imglist######

  6. def get_html(url):
  7.     res = requests.get(url)
  8.     return res.text

  9. def down(url):
  10.     url = pagelist[0] #设定url为pagelist列表中第一个
  11.     posttemp = re.compile(r'<a href="(/meituisiwatupian/\d+/\d+\.html)" t')
  12.     html = get_html(url)#定义html命令为获取url源代码
  13.     postlistx = re.findall(posttemp,html)
  14.     for post in postlistx:
  15.         post = 'http://www.tu11.com/' + str(post)
  16.         img = get_html(post)
  17.         imgfind = r'src="(http://img15.haotuwu.com:8080/picture/\d+/\w+[^"]+.jpg)"'
  18.         img = re.findall(imgfind,img)
  19.         for down in img:
  20.             filename = down.split("/")[-1]
  21.             urllib.request.urlretrieve(down,filename,None)
  22.         

  23. if __name__=='__main__':
  24.     page = 1
  25.     pagelist = []
  26.     while True:
  27.             if page > 10:
  28.                 break
  29.             url = "http://www.tu11.com/meituisiwatupian/list_2_" + str(page) + ".html"
  30.             pagelist.append(url)
  31.             page += 1
  32.     down(url)
复制代码

代码写的有点乱,基本思路是从main开始,构建一个页面列表pagelist,然后传入down函数,在down函数中构建一个postlistx列表,用拼接方式加上域名存为post列表,然后进入post列表中第一项,然后查找该帖子中所有.jpg

问题来了,现在程序跑起来的时候,仅得到了post列表中的第一个.jpg文件的地址,然后就去找post列表中第二个帖子去了。。。。

到底是哪里出错了呢?还是某个语句不该放在这个循环里?
最佳答案
2018-12-31 19:29:40
老实说,没明白你的意思。再说了人家网站也反爬。
如果你想爬所有页 应该是这样
  1. def down(urls):
  2.     for url in urls:
  3.         posttemp = re.compile(r'<a href="(/meituisiwatupian/\d+/\d+\.html)" t')
  4.         html = get_html(url)#定义html命令为获取url源代码
  5.         postlistx = re.findall(posttemp,html)
  6.         for post in postlistx:
  7.             post = 'http://www.tu11.com/' + str(post)
  8.             img = get_html(post)
  9.             imgfind = r'src="(http://img15.haotuwu.com:8080/picture/\d+/\w+[^"]+.jpg)"'
  10.             img = re.findall(imgfind,img)
  11.             for down in img:
  12.                 filename = down.split("/")[-1]
  13.                 urllib.request.urlretrieve(down,filename,None)
  14.         

  15. if __name__=='__main__':
  16.     page = 1
  17.     pagelist = []
  18.     while True:
  19.         if page > 10:
  20.             break
  21.         url = "http://www.tu11.com/meituisiwatupian/list_2_" + str(page) + ".html"
  22.         pagelist.append(url)
  23.         page += 1
  24.     down(pagelist)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-31 19:29:40 | 显示全部楼层    本楼为最佳答案   
老实说,没明白你的意思。再说了人家网站也反爬。
如果你想爬所有页 应该是这样
  1. def down(urls):
  2.     for url in urls:
  3.         posttemp = re.compile(r'<a href="(/meituisiwatupian/\d+/\d+\.html)" t')
  4.         html = get_html(url)#定义html命令为获取url源代码
  5.         postlistx = re.findall(posttemp,html)
  6.         for post in postlistx:
  7.             post = 'http://www.tu11.com/' + str(post)
  8.             img = get_html(post)
  9.             imgfind = r'src="(http://img15.haotuwu.com:8080/picture/\d+/\w+[^"]+.jpg)"'
  10.             img = re.findall(imgfind,img)
  11.             for down in img:
  12.                 filename = down.split("/")[-1]
  13.                 urllib.request.urlretrieve(down,filename,None)
  14.         

  15. if __name__=='__main__':
  16.     page = 1
  17.     pagelist = []
  18.     while True:
  19.         if page > 10:
  20.             break
  21.         url = "http://www.tu11.com/meituisiwatupian/list_2_" + str(page) + ".html"
  22.         pagelist.append(url)
  23.         page += 1
  24.     down(pagelist)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-10 15:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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