kygschp 发表于 2021-10-29 21:38:41

爬虫

有没有大哥知道下面的代码出了什么问题

import requests
import bs4

url = 'https://psychology.ku.dk/staff/academic_staff/'

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'}

response = requests.get(url, headers=headers)

soup = bs4.BeautifulSoup(response.text, 'html.parser')

name_targets = soup.find_all('td', class_=('emplistname', 'sorting_1'))
names=[]
for each in name_targets:
    name = each.a.text
    names.append(name)

title_targets = soup.find_all('td', class_="emplisttitle")
titles = []
for each in title_targets:
    title = each.text
    titles.append(title)
   
area_targets = soup.find_all('td', class_="emplistworkarea")
areas = []
for each in area_targets:
    area = each.text
    areas.append(area)
   
mail_targets = soup.find_all('td', class_="emplistemail")
mails = []
for each in mail_targets:
    mail = each.a['title']
    mails.append(mail)


KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_76524/2616645663.py in <module>
   31 mails = []
   32 for each in mail_targets:
---> 33   mail = each.a['title']
   34   mails.append(mail)

C:\ProgramData\Anaconda3\lib\site-packages\bs4\element.py in __getitem__(self, key)
   1484         """tag returns the value of the 'key' attribute for the Tag,
   1485         and throws an exception if it's not there."""
-> 1486         return self.attrs
   1487
   1488   def __iter__(self):

KeyError: 'title'

大马强 发表于 2021-10-30 09:03:32

a标签没有title这个属性
<a href="#" onclick="this.href=&#39;mai&#39; + &#39;lto:&#39; + &#39;ann-louise.holten&#39;+&#39;@&#39;+&#39;psy.ku&#39;+&#39;.&#39;+&#39;dk&#39;;return true;" onmouseover="this.
title很明显是onmouseover属性的值
页: [1]
查看完整版本: 爬虫