Python function data to another function HELP

Ramazan

Regular Member
Joined
Aug 19, 2018
Messages
441
Reaction score
310
Hello i have 2 functions and the names are sitemap and title I'm catching sitemap URL's on that function and with that URL going for the title function but I can't reach

for websiteurls in xml_urls:
return websiteurls
loop. That's my problem how can I reach that for loop?

Python:
def sitemap():
    url = 'https://url.com/sitemap.xml'
    sitemaprequest = requests.get(url)
    sitemapcontent = sitemaprequest.content
    sitemapsoup = BeautifulSoup(sitemapcontent,'lxml')
    sitemapurls = sitemapsoup.find_all("loc")
    xml_urls = []
    for sitemapurl in sitemapurls:
        xml_urls.append(sitemapurl.text)
        for websiteurls in xml_urls:
            return websiteurls
    
def title():
    
    for urlstitle in websiteurls:
        titlerequest = request.get(websiteurls)
        titlerequest.text
        titlesource = BeautifulSoup(titlerequest.text, 'html.parser')
        for titles in titlesource.find_all('title'):
            title = titles.get_text()
    return title
 
Should i use class or something for this?
 
Something like this could work. In the title function you need to add a parameter that accepts 'websiteurls' like done below. Another alternative could be to utilize global variables, but I would not recommend it.
Code:
def main():
    websiteurls = sitemap()
    title = title(websiteurls)

def sitemap():
    url = 'https://url.com/sitemap.xml'
    sitemaprequest = requests.get(url)
    sitemapcontent = sitemaprequest.content
    sitemapsoup = BeautifulSoup(sitemapcontent,'lxml')
    sitemapurls = sitemapsoup.find_all("loc")
    xml_urls = []
    for sitemapurl in sitemapurls:
        xml_urls.append(sitemapurl.text)
        for websiteurls in xml_urls:
            return websiteurls
    
def title(websiteurls):
    for urlstitle in websiteurls:
        titlerequest = request.get(websiteurls)
        titlerequest.text
        titlesource = BeautifulSoup(titlerequest.text, 'html.parser')
        for titles in titlesource.find_all('title'):
            title = titles.get_text()
    return title
 
Something like this could work. In the title function you need to add a parameter that accepts 'websiteurls' like done below. Another alternative could be to utilize global variables, but I would not recommend it.
Thanks for your answer.
Python:
def main():
    websiteurls = sitemap()
    title = title(websiteurls)
title = title(websiteurls) thing is not work. its giving an error and title(websiteurls) "title" is not defined error its have
 
Thanks for your answer.
Python:
def main():
    websiteurls = sitemap()
    title = title(websiteurls)
title = title(websiteurls) thing is not work. its giving an error and title(websiteurls) "title" is not defined error its have
Okay i found problem bcz title was function name that's why we had an problem now we have a different one. lol
titles() missing 1 required positional argument: 'websiteurls'
 
How about this?

Python:
def main():
    websiteurls = sitemap()
    titles = title(websiteurls)
    # return titles, print, send to json or something
   
def sitemap():
    url = 'https://url.com/sitemap.xml'
    sitemaprequest = requests.get(url)
    sitemapcontent = sitemaprequest.content
    sitemapsoup = BeautifulSoup(sitemapcontent,'lxml')
    sitemapurls = sitemapsoup.find_all("loc")
    xml_urls = []
    for sitemapurl in sitemapurls:
        xml_urls.append(sitemapurl.text)
        # for websiteurls in xml_urls:
        #     return websiteurls
    return xml_urls
   
def title(websiteurls):
    titles = []
    for urlstitle in websiteurls:
        titlerequest = request.get(websiteurls)
        titlerequest.text
        titlesource = BeautifulSoup(titlerequest.text, 'html.parser')
        for titles in titlesource.find_all('title'):
            title = titles.get_text()
            titles.append(title)
    return titles
 
Ya not sure why you would'nt be using class for this.
can easily store the variables in the self.variableName
then call it in the different function.
Someone helped me about this i'm beginner on python :)
 
Back
Top