在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 兩個方法不能互相引用?

兩個方法不能互相引用?

#-- conding:utf-8 --
import requests, re

def index_content(url_index, title=0, time=0):
    for url in url_index:
        index_req = requests.get(url)
        index_req.encoding = 'utf-8'
        index_content = index_req.text
        content_url = re.findall('<div class="text text-no-img">.*?<p class="title">.*?<a href="(.*?)" target="_blank">',index_content,re.S)
        title = re.search('<p class="title">.*?<a href=".*?" target="_blank">(.*?)</a>',index_content,re.S).group(1)
        time = re.search('<p class="time">(.*?)</p>',index_content,re.S).group(1).replace("年", "-").replace("月", "-").replace("日", "")
        print(content_url)
        print(title)
        print(time)
def content(content_url):
    for url in content_url:
        content_req = requests.get(url)
        content_req.encoding = 'utf-8'
        content = content_req.text
        return content
url_template = 'http://finance.eastmoney.com/news/cgnjj_{}.html'
url_index = [url_template.format(pages) for pages in range(1, 2)]

index_content(url_index, title=0, time=0)
content(content_url)

報錯 content_url 找不到。如果是兩個方法的話,怎么調(diào)用呢?
或者說,我這兩個方法寫好了,怎么讓它們一起工作...

回答
編輯回答
傻丟丟

2個方法可以相互引用,不過要做好退出條件限制,不能做成死循環(huán)。你這個跟相互調(diào)用沒有關(guān)系

2017年7月31日 10:28
編輯回答
挽青絲

最后一行content(content_url)中的這個content_url確實(shí)沒有定義啊,全局空間中沒有這個變量。

2017年10月23日 23:34
編輯回答
雨萌萌
def index_content(url_index,title=0,time=0):
    ...
    ...
    return content_url
...
content_url = index_content(url_index, title=0, time=0)
content(content_url)
2017年10月18日 15:05