如何在python中将多个项目附加到列表中

2024-10-01 19:15:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表,当我试图添加系统崩溃并显示以下错误时,我想使用python将多个项目附加到该列表中:

我已经尝试了append()函数extend()函数,但出现了相同的崩溃和错误


TypeError                                 Traceback (most recent call last)
<ipython-input-24-1e9480855366> in <module>
    121
    122 
--> 123     joineddd.extend(link,jobdesc,alldd)
    124 
    125 

TypeError: extend() takes exactly one argument (3 given)

代码:

import time

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(
    requests.get("https://www.bayt.com/en/international/jobs/executive-chef-jobs/").content,
    "lxml"
)

links = []
for a in soup.select("h2.m0.t-regular a"):
    if a['href'] not in links:
        links.append("https://www.bayt.com"+ a['href'])

joineddd = []

for link in links:
    print(link)
    s = BeautifulSoup(requests.get(link).content, "lxml")
    jobdesc=s.select_one("div[class='card-content is-spaced'] p")
    print(jobdesc.text)
    alldt = [dt.text for dt in s.select("div[class='card-content is-spaced'] dt")]
    dt_Job_location =              alldt[0]
    dt_Job_Company_Industry =      alldt[1]
    dt_Job_Company_Type =          alldt[2]
    if len(alldt[3])>0:
        dt_Job_Job_Role =              alldt[3]
    elif len(dt_Job_Employment_Type)>0:
        dt_Job_Employment_Type =       alldt[4]
    
    alldd = [dd.text for dd in s.select("div[class='card-content is-spaced'] dd")]
    dd_job_location =             alldd[0]
    dd_job_Company_Industry =     alldd[1]
    dd_job_Company_Type =         alldd[2]
    if len(alldd[3])>0:
        dd_job_Job_Role =             alldd[3]
    elif len(dd_job_Employment_Type)>0:
        dd_job_Employment_Type =      alldd[4]
    
    print(f"{dt_Job_location}:{dd_job_location}\n{dt_Job_Company_Industry}:{dd_job_Company_Industry}\n\n")
    
    print("-" * 80) 


    
    joineddd.extend(link,jobdesc,alldd)

预期结果:

[link,description,location,Company_Industry,Company_Type,Job_Role,Employment_Type ]

Tags: intypedtlinkjoblocationcontentcompany
3条回答

您可以使用list.extend方法,但在项目周围使用方括号,如下所示:

list = [1,2,3]

list.extend([4,5])

print(list)

>> returns 1,2,3,4,5
import time

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(
    requests.get("https://www.bayt.com/en/international/jobs/executive-chef-jobs/").content,
    "lxml"
)

links = []
for a in soup.select("h2.m0.t-regular a"):
    if a['href'] not in links:
        links.append("https://www.bayt.com"+ a['href'])

joineddd = []

for link in links:
    print(link)
    s = BeautifulSoup(requests.get(link).content, "lxml")
    jobdesc=s.select_one("div[class='card-content is-spaced'] p")
    print(jobdesc.text)
    alldt = [dt.text for dt in s.select("div[class='card-content is-spaced'] dt")]
    dt_Job_location =              alldt[0]
    dt_Job_Company_Industry =      alldt[1]
    dt_Job_Company_Type =          alldt[2]
    if len(alldt[3])>0:
        dt_Job_Job_Role =              alldt[3]
    elif len(dt_Job_Employment_Type)>0:
        dt_Job_Employment_Type =       alldt[4]
    
    alldd = [dd.text for dd in s.select("div[class='card-content is-spaced'] dd")]
    dd_job_location =             alldd[0]
    dd_job_Company_Industry =     alldd[1]
    dd_job_Company_Type =         alldd[2]
    if len(alldd[3])>0:
        dd_job_Job_Role =             alldd[3]
    elif len(dd_job_Employment_Type)>0:
        dd_job_Employment_Type =      alldd[4]
    
    print(f"{dt_Job_location}:{dd_job_location}\n{dt_Job_Company_Industry}:{dd_job_Company_Industry}\n\n")
    
    print("-" * 80) 


    
    joineddd.extend([link,jobdesc,alldd])

数据帧:

import pandas as pd
import numpy as np
array1 = ["value1", "value2"]
array2 = ["value1"]

df = dict( A = np.array(array1), B = np.array(array2 ) )

_df = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in df.items() ]))


print(_df)
_df.to_csv("filename.csv", index=False, encoding="utf-8")

输出:

        A       B
0  value1  value1
1  value2     NaN

对于扩展功能,您需要提供一个列表/touple/dict/set来添加多个项

例如:

joineddd.extend([link,jobdesc,alldd])

相关问题 更多 >

    热门问题