使用python和beautifulsoup捕获来自td标签的数据

妮可·巴拉塔(Nicole C.Baratta)

我是Python的初学者,正在使用我熟悉的数据来完成一些任务,以学习基础知识。我正在尝试通过表格爬网以收集联系信息,但是在获取tds列表中的数据时遇到了问题。

HTML看起来像这样:

<table class="table table-striped" data-drupal-selector="edit-directory" id="edit-directory--zJwP9mT4moQ">
   <thead>
   <tr>
       <th>Name</th>
       <th>Job Title</th>
       <th>Campus/Department</th>
       <th>Contact</th>
   </tr>
   </thead>
   <tbody>
   <tr class="odd">
       <td>LAST, FIRST</td>
       <td>T-HS SCI- GEN'L</td>
       <td><span tabindex="0">SCHOOL</span></td>
       <td><a href="mailto:[email protected]" class="email"><span aria-hidden="true">Email</span><span class="sr-only">[email protected]</span></a><br>555-555-5555</td>
   </tr>
</table>

我有这段代码来获取表格

data = urllib.parse.urlencode(params).encode("utf-8")
    req = urllib.request.Request(url)
    with urllib.request.urlopen(req,data=data) as f:
        soup = bs(f, 'html.parser')

table = soup.find("table")

for row in table.findAll("tr"):
        #print (row)
        cells = row.findAll("td")
        print(cells) 

我得到这样的东西:

[<td>LAST,FIRST </td>, <td>TEMP PROF</td>, <td><span tabindex="0">SCHOOL</span></td>, <td><a class="email" href="mailto:[email protected]"><span aria-hidden="true">Email</span><span class="sr-only">[email protected]</span></a><br/>555-555-5555</td>]

[<td><a href="https://teachersite.com" target="_blank">LAST, FIRST</a></td>, <td>T-ENGLISH</td>, <td><span tabindex="0">SCHOOL</span></td>, <td><a class="email" href="mailto:[email protected]"><span aria-hidden="true">Email</span><span class="sr-only">[email protected]/span></a><br/>555-555-5555</td>]

但是,如果我尝试获取列表中的数据:

print (cells[1]) 

表示索引超出范围

我想要得到的是这样的:

last = 'LAST'
first = 'FIRST'
email = '[email protected]'
title = 'TEMP PROF'
phone = '555-555-5555'
伊库尔斯基

似乎您想从每个元素中删除文本:

for row in table.findAll('tr'):
    cols = row.findAll('td')
    cols = [element.text.strip() for element in cols]
    for col in cols:
        print(col)

要查找名字和姓氏,可以使用:将第一个元素除以逗号和空格.split(', ')希望这为您指明了正确的方向!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用beautifulSoup从<td>标签分别抓取数据?

如何使用Python BeautifulSoup提取td HTML标签?

使用 Python 和 BeautifulSoup 抓取 alt 标签

使用beautifulsoup从“td”标签解析整数

使用beautifulsoup获取多个标签和属性数据

使用 Python 和 BeautifulSoup 提取 CME 数据

使用BeautifulSoup 4和递归来捕获HTML嵌套标签的结构

使用Python和Beautiful Soup如何捕获空标签

Python BeautifulSoup - 使用 <div> 之间的 html 标签创建数据框

使用Python Beautifulsoup从复杂的html标签获取数据

使用BeautifulSoup4在Python中存储标签中的数据

使用BeautifulSoup和Python从item标签获取地址文本

如何使用beautifulsoup和python在span标签中获取文本

使用 python 和 BeautifulSoup 抓取不完整的标签

使用 Python、Selenium 和 BeautifulSoup 来抓取标签的内容?

如何使用Python和Beautifulsoup从脚本标签获取JavaScript变量

如何使用Python和BeautifulSoup中的类过滤标签?

如何使用 python 和 BeautifulSoup 获取标签内的文本

Python - BeautifulSoup - 根据数据标题整理 <td>

如何使用BeautifulSoup在<tr>中捕获特定的<td>

无法解析来自`th`标签的数据以及来自不同表的`td`标签

在Wiki上使用BeautifulSoup和python使用“ tr”和“ td”进行搜寻

在python中以特定宽度存储来自td标签的信息

使用PHP在td标签之间获取数据

使用Linq和来自数据库的数据来嵌套XML标签

Python BeautifulSoup按索引对td标签进行排序,[0]和[2]有效,但[1]不起作用

使用BeautifulSoup在结果集中获取td标签的_text

如何使用 Selenium 和 Python 从表中捕获隐藏数据?

Python-如何使用“ requests&BeautifulSoup”抓取Tr / Td表数据