Home > Blockchain >  I'm finding it hard to understand how functions work. Would someome mind explaining them?
I'm finding it hard to understand how functions work. Would someome mind explaining them?

Time:12-03

Please excuse the extra modulus. I've taken a small part of my code out to convert it into functions to make my code less messy. However I'm finding it really hard to understand how I put values in and take them out to print or do things with. See the code I'm using below. VideoURL would be replaced with a url of a video.

`

from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
from pytube import YouTube
from pytube import Channel


channelURL = "videoURL"
YouTubeDomain = "https://www.youtube.com/channel/"


def BeautifulSoup(Link):
    soup = BeautifulSoup(requests.get(Link, cookies={'CONSENT': 'YES 1'}).text, "html.parser")
    data = re.search(r"var ytInitialData = ({.*});", str(soup.prettify())).group(1)
    json_data = json.loads(data)

    channel_id   = json_data["header"]["c4TabbedHeaderRenderer"]["channelId"]
    channel_name = json_data["header"]["c4TabbedHeaderRenderer"]["title"]
    channel_logo = json_data["header"]["c4TabbedHeaderRenderer"]["avatar"]["thumbnails"][2]["url"]
    channel_id_link = YouTubeDomain channel_id
    print("Channel ID: " channel_id)
    print("Channel Name: " channel_name)
    print("Channel Logo: " channel_logo)
    print("Channel ID: " channel_id_link)

    
def vVersion(*arg):
    YTV = YouTube(channelURL)
    channel_id = YTV.channel_id
    channel_id_link = YTV.channel_url

    c = Channel(channel_id_link)
    channel_name =c.channel_name
    return channel_id_link, channelURL

channel_id_link, video = vVersion()
print(channel_id_link)
Link = channel_id_link
print(Link)
Test = print(BeautifulSoup(Link))
Test()

    
    

So the errors I keep getting are about having too many or too few args for the functions .

Here's the current error: `

BeautifulSoup() takes 1 positional argument but 2 were given
  File "C:\Users\Admin\test\video1.py", line 26, in BeautifulSoup
    soup = BeautifulSoup(requests.get(Link, cookies={'CONSENT': 'YES 1'}).text, "html.parser")
  File "C:\Users\Admin\test\video1.py", line 53, in <module>
    Test = print(BeautifulSoup(Link))

`I know I'm missing something very simple.

Any help would be welcome, thank you! `

I have tried to take the code out of my main code to isolate the issue.

I was expecting to gain a perspective on the issue.

I tried the following code to train myself on functions but it didn't really help me fix the issue I'm having with my project.




def test():
    name = (input("Enter your name?"))
    favNumber = (input("Please enter your best number?"))
    return name, favNumber

name, favNumber = test()
print(name)
print(float(favNumber))
    



    
    

CodePudding user response:

It's because you have named your function as BeautifulSoup which is as same as the name of the function from the library you have imported. Instead of using the function BeautifulSoup from bs4, it is now running the code you have defined which takes only one argument. So give your function another name.

  • Related