我有这个TypeError:当调用我之前定义的函数时,'list'对象在for循环中不可调用。我想将函数追加/注入我的for循环,以便按列自动填充csv行
import requests
import bs4
from bs4 import BeautifulSoup
import pandas as pd
import time
import csv
# copy and paste the url from indeed using your search term
URL = 'https://www.indeed.com/jobs?q=data+science+summer+intern&l=New+York'
# conducting a request of the stated URL above:
page = requests.get(URL)
# specifying a desired format of “page” using the html parser - this allows python to read the various components of the page, rather than treating it as one long string.
soup = BeautifulSoup(page.text, 'html.parser')
# printing soup in a more structured tree format that makes for easier reading
print(soup.prettify())
这个extract_job_title_from_result()函数实际上从“作业标题”获取并附加到“作业”列表。
def extract_job_title_from_result(soup):
jobs = []
for div in soup.find_all(name='div', attrs={'class':'row'}):
for a in div.find_all(name='a', attrs={'data-tn-element':'jobTitle'}):
jobs.append(a['title'])
return(jobs)
extract_job_title_from_result = extract_job_title_from_result(soup)
print('extract_job_title_from_result is: ', extract_job_title_from_result)
解决办法:在extract_job_title_from_result = extract_job_title_from_result(soup),您已将结果替换为该函数extract_job_title_from_result,该结果是一个列表。
因此,下次尝试调用它时,extract_job_title_from_result不再是函数的名称,而是引用此列表。
使用其他名称,例如:
job_title = extract_job_title_from_result(soup)
print('job_title is: ', job_title)








暂无数据