无法将字符串和列表从一个函数返回到另一个函数

我在 python 中创建了一个脚本来解析website address不同机构的登陆页面和location address内部页面。我无法理解的是如何同时返回 astring和 alist以便它们在另一个函数中重用。更清楚一点:我希望从函数中返回website addressand 并list of links在collect_links()函数中重用它们get_info()。我目前的方法会引发错误 - ValueError: not enough values to unpack (expected 2, got 1).


到目前为止,这是我的尝试:


import re

import requests

from bs4 import BeautifulSoup

from urllib.parse import urljoin


def collect_links(link):

    res = requests.get(link)

    soup = BeautifulSoup(res.text, "lxml")

    website = [soup.select_one("p.company-profile-website > a").get("href")]

    items = [urljoin(url,item.get("href")) for item in soup.select("[id^='company-'] .search-companies-result-info h2 > a")]

    return website,items


def get_info(website,link):

    res = requests.get(link)

    soup = BeautifulSoup(res.text, "lxml")

    address = soup.select_one("p.footer-right").get_text(strip=True)

    print(website,address)


if __name__ == '__main__':

    url = "https://www.cv-library.co.uk/companies/agencies/A"

    for item,link in collect_links(url):

        get_info(item,link)

如何将字符串和列表从一个函数返回到另一个函数?


PS 我想坚持我已经尝试过的设计。


哈士奇WWW
浏览 121回答 3
3回答

慕妹3242003

你websites是一个list带有单个元素的字符串,而不是你用[]文字括起来的字符串。您需要删除[]以使其成为字符串,因为没有必要将其设为列表。这样做之后,您可以获得返回值,并遍历链接,例如:if __name__ == '__main__':    url = "https://www.cv-library.co.uk/companies/agencies/A"    website, links = collect_links(url)     for link in links:        get_info(website, link)

慕码人2483693

代码中的主要错误在此链接中。website = [soup.select_one("p.company-profile-website > a").get("href")]这仅返回一个值:http://www.autoskills-uk.com你的功能应该是:def collect_links(link):&nbsp; &nbsp; res = requests.get(link)&nbsp; &nbsp; soup = BeautifulSoup(res.text, "lxml")&nbsp; &nbsp; websites = [x.get("href") for x in soup.select("p.company-profile-website > a")]&nbsp; &nbsp; #<============== Changed&nbsp; &nbsp; items = [urljoin(url,item.get("href")) for item in soup.select("[id^='company-'] .search-companies-result-info h2 > a")]&nbsp; &nbsp; return zip(websites, items)作为网站和项目的 zip 返回。现在您可以在 for 循环中列出 unpackitem和link:if __name__ == '__main__':&nbsp; &nbsp; url = "https://www.cv-library.co.uk/companies/agencies/A"&nbsp; &nbsp; for item,link in collect_links(url):&nbsp; &nbsp; &nbsp; &nbsp; get_info(item,link)

白板的微信

您将返回两个列表,一个包含一个元素,另一个包含多个元素作为元组,并尝试遍历此元组,将每个列表解包为两个元素item和link.我不明白你真正想要做什么,但你应该将 for 循环和返回值分开:website, links = collect_links(url)for link in links:&nbsp; &nbsp; get_info(website[0], link)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python