鱼C论坛

 找回密码
 立即注册
查看: 574|回复: 5

[已解决]for in基础问题求助

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

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

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

x
各位大神好,我又来了。请教一下,先贴代码
  1. print(postlist)
  2.             for imgx in postlist:
  3.                 print(imgx)
复制代码


postlist是一个列表,其中是http://xxx.xxx.xxx/xxx/xxx/xxx.html这样的格式,可是for in了以后imgx列表中,变成了:
h
t
t
p
:
/
/
变成这样的格式了。

我试过for imgx in postlist以后 imgx = str(imgx)也不行。

求解决办法
最佳答案
2018-12-31 23:43:05
jasmorning 发表于 2018-12-31 23:36
我好像看懂你说的了,因为在for循环里,所以变成字符串了吗?

def get_url(postlist): # 这里什么时候成postlist了? 这明明传进来就是一个http字符串
20181231_234018.png
你想传列表请挑好缩进

  1.         for postlist in postlistx:
  2.             postlist = r'http://www.tu11.com' + str(postlist)
  3.             html = get_html(url)
  4.             print(postlist)
  5.         get_url(postlist)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-31 23:22:43 | 显示全部楼层
你就放2句代码上来,凭什么你这么肯定你的postlist是列表?
20181231_232147.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-31 23:25:54 | 显示全部楼层
本帖最后由 jasmorning 于 2018-12-31 23:32 编辑
  1. import requests
  2. import urllib.request
  3. import re

  4. def get_html(url):
  5.     res = requests.get(url)
  6.     res.encoding = res.apparent_encoding
  7.     return res.text

  8. def get_list():
  9.     page = 1
  10.     while True:
  11.         if page > 2:
  12.             break
  13.         url = 'http://www.tu11.com/meituisiwatupian/list_2_' + str(page) + '.html'
  14.         html = get_html(url)
  15.         postfind = r'<a href="([^"]+.html)" t'
  16.         postlistx =re.findall(postfind,html)
  17.         for postlist in postlistx:
  18.             postlist = r'http://www.tu11.com' + str(postlist)
  19.             html = get_html(url)
  20.             get_url(postlist)
  21.             print(postlist)
  22.         page += 1


  23. ###postlist列表中打开,匹配下一页,匹配jpg,循环。
  24. def get_url(postlist):
  25.     for imgx in postlist:
  26.         print(imgx)

  27. get_list()
复制代码

sorry!sorry!
因为第一个print(postlist)的打印结果正常,进入第二个函数的for循环就变样了。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-31 23:36:25 | 显示全部楼层
ba21 发表于 2018-12-31 23:22
你就放2句代码上来,凭什么你这么肯定你的postlist是列表?

我好像看懂你说的了,因为在for循环里,所以变成字符串了吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-31 23:43:05 | 显示全部楼层    本楼为最佳答案   
jasmorning 发表于 2018-12-31 23:36
我好像看懂你说的了,因为在for循环里,所以变成字符串了吗?

def get_url(postlist): # 这里什么时候成postlist了? 这明明传进来就是一个http字符串
20181231_234018.png
你想传列表请挑好缩进

  1.         for postlist in postlistx:
  2.             postlist = r'http://www.tu11.com' + str(postlist)
  3.             html = get_html(url)
  4.             print(postlist)
  5.         get_url(postlist)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-31 23:57:50 | 显示全部楼层
ba21 发表于 2018-12-31 23:43
def get_url(postlist): # 这里什么时候成postlist了? 这明明传进来就是一个http字符串

你想传列表请 ...

如果不传列表的话,for imgx in postlist是不是就不起作用了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-10 06:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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