import requests
from bs4 import BeautifulSoup
import csv
from itertools import zip_longest
job_title = []
company_name = []
location_name = []
job_skill = []
links = []
salary = []
result = requests.get("https://www.indeed.com/jobs?q=web development&l=&from=searchOnHP")
source = result.content
soup = BeautifulSoup(source, "lxml")
job_titles = soup.find_all("a", {"class", "jcs-JobTitle"})
company_names = soup.find_all("span", {"class": "companyName"})
location_names = soup.find_all("div", {"class": "companyLocation"})
job_skills = soup.find_all("div", {"class": "job-snippet"})
for i in range(len(job_titles)):
job_title.append(job_titles[i].text.strip())
links.append("https://www.indeed.com" job_titles[i].attrs["href"])
company_name.append(company_names[i].text.strip())
location_name.append(location_names[i].text.strip())
job_skill.append(job_skills[i].text.strip())
for link in links:
result = requests.get(link)
source = result.content
soup = BeautifulSoup(source, "lxml")
salaries = soup.find("span", {"class": "icl-u-xs-mr--xs attribute_snippet"})
salary.append(salaries.text)
my_file = [job_title, company_name, location_name, job_skill, salary]
exported = zip_longest(*my_file)
with open("/Users/Rich/Desktop/testing/indeed.csv", "w") as myfile:
writer = csv.writer(myfile)
writer.writerow(["Job titles", "Company names", "Location names", "Job skills", "salaries"])
writer.writerows(exported)
我正在抓取這個網站以通過抓取它們的每一頁來獲取職位名稱、公司名稱、位置名稱、作業技能和薪水,當我列印推薦時它可以作業:salary = soup.find("span", {" class": "icl-u-xs-mr--xs attribute_snippet"}) 但是當我嘗試僅附加其中的文本時,我收到此錯誤: AttributeError: 'NoneType' object has no attribute 'text' 請幫我解決這個問題,我們將不勝感激。
uj5u.com熱心網友回復:
主要問題是并不總是有薪水,所以你必須處理這個,例如,if condition你也可以避免所有這些串列。
salary.text if salary else None
例子
import requests
from bs4 import BeautifulSoup
result = requests.get("https://www.indeed.com/jobs?q=web development&l=&from=searchOnHP")
source = result.content
soup = BeautifulSoup(source, "lxml")
data = []
for e in soup.select('ul.jobsearch-ResultsList .slider_item'):
salary = e.find("div",{"class": "salary-snippet-container"})
data.append({
'title': e.find("a", {"class", "jcs-JobTitle"}).get_text(strip=True),
'company': e.find("span", {"class": "companyName"}).get_text(strip=True),
'location': e.find("div", {"class": "companyLocation"}).get_text(strip=True),
'skills': e.find("div", {"class": "job-snippet"}).get_text(strip=True),
'salary': salary.text if salary else None
})
with open('indeed.csv', 'w', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = data[0].keys())
writer.writeheader()
writer.writerows(data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483250.html
上一篇:如何抓取跨度兄弟跨度的文本?
