首页 新闻 会员 周边 捐助

Python 自定义函数,函数内输出正常,调用函数时只输出一条

0
[已解决问题] 解决于 2018-07-26 14:35
 1 def start_url(url):
 2         nianduContent = requests.get(url).text #获取网页源代码
 3         nian = re.findall('<option value=".*?".*?>(.*?)</option>', nianduContent, re.S) #获取年份
 4         # print(nian)
 5         for n in nian:
 6             if '季度' not in n and '-' not in n: #过滤年份中包含‘季度’和‘-’的
 7                 # print(n)
 8                 jidu = range(1, 5) #1~4 四个季度
 9                 for j in jidu:
10                     # print(j)
11                     # for urls in url:
12                     urls = url + str(n) + '&season=' + str(j) #拼接url
13                     # print(urls)
14         return urls
输出结果:
http://quotes.****.***.com/trade/lsjysj_*******.html?year=1999&season=1
http://quotes.****.***.com/trade/lsjysj_*******.html?year=1999&season=2
http://quotes.****.***.com/trade/lsjysj_*******.html?year=1999&season=3
http://quotes.****.***.com/trade/lsjysj_*******.html?year=1999&season=4
....
http://quotes.****.***.com/trade/lsjysj_*******.html?year=1999&season=4

代码如上,函数内运行的都很正常,循环的也正常

但是调用的时候,就只输出一条

调用:

1 url = 'http://quotes.*****.***.com/trade/lsjysj_******.html?year='
2 s = start_url(url)
3 print(s)
输出结果:
http://quotes.****.***.com/trade/lsjysj_*******.html?year=1999&season=4
会发光的主页 会发光 | 菜鸟二级 | 园豆:258
提问于:2018-07-26 10:05
< >
分享
最佳答案
0
def start_url(url):
        nianduContent = requests.get(url).text #获取网页源代码
        nian = re.findall('<option value=".*?".*?>(.*?)</option>', nianduContent, re.S) #获取年份
        urls=list()
        # print(nian)
        for n in nian:
            if '季度' not in n and '-' not in n: #过滤年份中包含‘季度’和‘-’的
                # print(n)
                jidu = range(1, 5) #1~4 四个季度
                for j in jidu:
                    # print(j)
                    # for urls in url:
                    tempurl = url + str(n) + '&season=' + str(j) #拼接url
                    urls.append(tempurl)
                    # print(urls)
        return urls
url = 'http://quotes.*****.***.com/trade/lsjysj_******.html?year='
s = start_url(url)
print(s)

声明list类型的urls=list()

奖励园豆:5
悟行 | 专家六级 |园豆:12559 | 2018-07-26 13:32

为啥我直接用urls = []不行呢,也是一个空列表呀

会发光 | 园豆:258 (菜鸟二级) | 2018-07-26 13:40
其他回答(1)
0
def start_url(url):
        nianduContent = requests.get(url).text #获取网页源代码
        nian = re.findall('<option value=".*?".*?>(.*?)</option>', nianduContent, re.S) #获取年份

        urls=[]

        # print(nian)
        for n in nian:
            if '季度' not in n and '-' not in n: #过滤年份中包含‘季度’和‘-’的
                # print(n)
                jidu = range(1, 5) #1~4 四个季度
                for j in jidu:
                    # print(j)
                    # for urls in url:
                    tempurl = url + str(n) + '&season=' + str(j) #拼接url
                    urls.append(tempurl)
                    # print(urls)
        return urls
url = 'http://quotes.*****.***.com/trade/lsjysj_******.html?year='
s = start_url(url)
print(s)
DanBrown | 园豆:1321 (小虾三级) | 2018-07-26 13:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册