Python 网络垃圾:错误';非类型';对象没有属性';文本';
作为一个项目工作的一部分,我正在努力清理这个网站。我在尝试获取职位摘要时遇到“NoneType”对象没有属性“text” 有人有办法吗Python 网络垃圾:错误';非类型';对象没有属性';文本';,python,web-scraping,Python,Web Scraping,作为一个项目工作的一部分,我正在努力清理这个网站。我在尝试获取职位摘要时遇到“NoneType”对象没有属性“text” 有人有办法吗 def extract(page): headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'} url = f'http
def extract(page):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
url = f'https://ae.indeed.com/jobs?q=data+analyst&l=dubai&start={page}'
r = requests.get(url, headers)
soup = bs(r.content, 'html.parser')
return soup
def transform(soup):
div = soup.find_all('div', class_ ='jobsearch-SerpJobCard')
for item in div:
title = item.find('a').text.strip()
company = item.find('span', class_ = 'company').text.strip()
summary = item.find(name='div',attrs={'class':'summary'}).text.strip()
jobs = {'title': title,
'company': company,
'summary': summary}
joblist.append(jobs)
return
joblist = []
c = extract(10)
transform(c)
print(joblist)
错误消息:
AttributeError Traceback (most recent call last)
<ipython-input-65-eefd76c3693a> in <module>
24 joblist = []
25 c = extract(10)
---> 26 transform(c)
27 print(joblist)
<ipython-input-65-eefd76c3693a> in transform(soup)
11 title = item.find('a').text.strip()
12 company = item.find('span', class_ = 'company').text.strip()
---> 13 summary = item.find(name='div',attrs={'class':'summary'}).text.strip()
14 # summary = item.find(name='li',attrs={'style':'margin-bottom:0px;'}).text
15 # for sum in summary:
AttributeError: 'NoneType' object has no attribute 'text'
AttributeError回溯(最近一次调用)
在里面
24工作清单=[]
25 c=提取物(10)
--->26(c)
27打印(工作清单)
变换(汤)
11 title=item.find('a').text.strip()
12 company=item.find('span',class='company')。text.strip()
--->13 summary=item.find(name='div',attrs={'class':'summary'}).text.strip()
14#summary=item.find(name='li',attrs={'style':'margin-bottom:0px;'}).text
15#汇总金额:
AttributeError:“非类型”对象没有属性“文本”
我只是尝试一下修改了代码,除了块。它工作得很好
try:
summary = item.find(name='div',attrs={'class':'summary'}).text.strip()
except:
summary = 'None'
那么,您是否希望item.find(name='div',attrs={'class':'summary'})总是返回一些内容?如果没有summary呢?item找不到div,class=summary,所以它返回NoneTypeobject@OneCricketeer谢谢你的提问。它帮助了我。