简体   繁体   中英

I am unable to use pass variables from one function to another

I am unable to use pass variables from one function to another. I get the following error message: ValueError: too many values to unpack (expected 3) Here's my code:

import requests
from bs4 import BeautifulSoup
from discord_webhook import DiscordWebhook, DiscordEmbed


def data():
    s = requests.session()
    s.headers.update({'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'})
    url = ''
    r = s.get(url)
    soup = BeautifulSoup(r.text, 'lxml')
    j = []
    k = []
    for i in soup.find("[class=class] a"):
        m = i['id']
        n = i['href']
    j.append(m)
    k.append(n)
    return url, soup, j, k

def more_data():
    soup = data()
    t = soup.find(id="title").text
    d = soup.find(id="description").text
    return t, d

def main():
    url, j, k = data()
    t, d = more_data()

    webhook = DiscordWebhook(url='')
    embed = DiscordEmbed(title='{}'.format(t), url='{}'.format(url), description='{}'.format(d)
    embed.add_embed_field(name='j', value='{}'.format(i) for i in j)
    embed.add_embed_field(name='k', value='{}'.format(i) for i in k)
    webhook.add_embed(embed)
    webhook.execute()

    if __name__ == "__main__":
        main()

I have also tried calling other functions inside main() but that did not work either.

Just use an underscore "_" for a variable which will not be in use, as in the Python language one of the meaning for the underscores is ignoring the specific values.

def data():
    ...
    return url, soup, j, k

url, _, j, k = data()

or if you have even more vars which you wanna skip

def data():
    ...
    return url, soup, soup2, soup3, j, k

url, *_, j, k = data()

regarding of your question of improvement:

import requests
from bs4 import BeautifulSoup
from discord_webhook import DiscordWebhook, DiscordEmbed

def get_page(url):
    s = requests.session()
    s.headers.update(
        {
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
        }
    )
    try:
        resp = s.get(url)
    except:
        return None
    else:
        return soup = BeautifulSoup(resp.text, 'lxml') 

def data(soup):
    look_for = '[class=class] a'
    def populate(item):
        return { 'id': item['id'], 'href': item['href'] }
    return [ populate(i) for i in soup.find(look_for) ]

def more_data(soup):
    title = soup.find(id="title").text
    description = soup.find(id="description").text
    return title, description

def main():
    url = ''
    soup = get_page(url)
    if not soup:
        return print('No page contents.')

    links = data(soup)
    title, description = more_data(soup)

    webhook = DiscordWebhook(url='')
    embed = DiscordEmbed(title=f'{ title }', url=f'{ url }', description=f'{ description }')
    for link in links:
        embed.add_embed_field(name='j', value=f'{ link['id'] }' )
        embed.add_embed_field(name='k', value=f'{ link['href'] }' )
    webhook.add_embed(embed)
    webhook.execute()


if __name__ == "__main__":
    main()

I'm not sure about embed.add_embed_field if it's correct coz never used discord_webhook module and have no chance to test it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM