将函数转换为列表

时间:2018-05-20 00:53:17

标签: python python-3.x beautifulsoup

我正在尝试将xml站点地图转换为列表。最终,我不知道如何拨打每一条线路。如果我不能打电话,例如lst[0]lst[1]我无法抓住每个链接,例如title = (get_url()).findAll("div",{"class":"large-7 medium-9 columns"}[0].h1)

教我如何使用

from bs4 import BeautifulSoup
import requests


def get_urls():
    baseurl = 'https://link/sitemap.xml'
    request = requests.get(baseurl)
    response = str(request.content)
    soup = BeautifulSoup(response, 'html.parser')
    search_html = soup.find_all('url')
    results_list = [item.find('loc').decode().split('>', 1)[1].split('<', 1)[0].strip() for item in search_html]
    for url in results_list:
        retuned_objects(url)


def retuned_objects(url):
    print(format(url))

# i = []

# for i in get_urls():
# lnks

lst = [get_urls()]

print(lst[0])

1 个答案:

答案 0 :(得分:2)

是的,只需返回results_list

from bs4 import BeautifulSoup
import requests

def get_urls():
    baseurl = 'https://www.desertessence.com/sitemap.xml'
    request = requests.get(baseurl)
    response = str(request.content)
    soup = BeautifulSoup(response, 'html.parser')
    search_html = soup.find_all('url')
    results_list = [item.find('loc').decode().split('>', 1)[1].split('<', 1)[0].strip() for item in search_html]
    return results_list

lst = get_urls()

print(lst)