【问题标题】:Python calling methods with variablesPython调用带有变量的方法
【发布时间】:2017-10-08 08:59:05
【问题描述】:

不知道标题是否准确。
我有 5 种方法可以抓取不同的网站。每个函数看起来像这样:

def getWebsiteData1(last_article):
    ty = datetime.today()
    ty_str = ty.strftime('%d.%m.%Y')
    url = 'http://www.website.com/news'
    r = requests.get(url)
    html = r.text
    soup = BeautifulSoup(html, 'html.parser')
    articles = soup.findAll("div", {"class": "text"})[:15]
    data = list()
    for article in articles:
        article_data = dict()
        if article.find("a").get('href') == last_article:
            return data
        else:
            article_data["link"] = article.find("a").get('href')
            article_data["title"] = article.find("a").get_text()
            data.append(article_data)
    return data

所以每个函数都会返回一个字典列表。
我有另一个调用这个函数的函数:

def CreateArticle(website_number, slug):
    website = Website.objects.get(slug=slug)
    last_article = website.last_article
    data = getWebsiteData1(last_article) # here i want to do something like
    data = website_number(last_article) # but ofcourse this doesnt work 
    if len(data) == 0:
        return "No news"
else:
    for i in data:
        article = Article(service=service)
        article.title = i['title']
        article.url = i['link']
        article.code = i['link']
        article.save()
    service.last_article = data[0]['link']
    service.save(update_fields=['last_article'])
    return data[0]['link']

我希望能够调用 CreateArticle(website_number) 并告诉这个函数它应该调用哪个 getWebsiteData 函数,所以我只能有一个 CreateArticle 函数,而不是每个 webscraper 函数都有另一个 CreateArticle 函数。 我希望我的问题很清楚:D

【问题讨论】:

    标签: python django variables methods


    【解决方案1】:

    在python中,函数是第一类的,可以作为参数传递给其他函数。

    def a():
        print("x")
    
    def b(some_function):
        some_function()
    

    然后

    b(a)
    

    将打印“x”,因为 a 在 b 中被调用。

    这样你就可以确定你要使用什么功能,然后将其传入使用。

    【讨论】:

      猜你喜欢
      • 2021-03-15
      • 2017-06-11
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多