我在 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 我想坚持我已经尝试过的设计。
慕妹3242003
慕码人2483693
白板的微信
相关分类