python - 缩小我使用 python 从网站上抓取的内容
问题描述
我正在尝试为网站练习我的 python 抓取,但是在 python 无法识别我所要求的内容的情况下,我无法将其缩小到合理的大小。例如,这是我的代码:
import bs4
import requests
url = requests.get('https://ballotpedia.org/Alabama_Supreme_Court')
soup = bs4.BeautifulSoup(url.text, 'html.parser')
y = soup.find('table')
print(y)
我正在尝试获取阿拉巴马州最高法院法官的姓名,但使用此代码,我得到的信息太多了。我尝试过诸如(第6行)之类的东西
y = soup.find('table',{'class':'wikitable sortable'})`
但我收到一条消息说搜索没有找到结果。
这是网页检查的图像。我的目标是让thead在我的代码中工作,但失败了!
如何向 python 指定我只想要评委的姓名?
非常感谢!
解决方案
简单地说,我会这样做。
import pandas as pd
df = pd.read_html("https://ballotpedia.org/Alabama_Supreme_Court")[2]["Judge"]
print(df.to_list())
输出:
['Brad Mendheim', 'Kelli Wise', 'Michael Bolin', 'William Sellers', 'Sarah Stewart', 'Greg Shaw', 'Tommy Bryan', 'Jay Mitchell', 'Tom
Parker']
现在回到原来
issue
的解决它,因为我个人喜欢解决真正的问题,而不是导航到替代解决方案。
有区别find
which 将只返回第一个element
但find_all
将返回 a list
of elements
。检查文档。
直接导入from bs4 import BeautifulSoup
而不是import bs4
因为它是 Python的 DRY 原则。
留下bs4
来处理内容,因为它是后台的任务之一。所以而不是r.text
使用r.content
现在,我们将深入到HTML
选择它:
from bs4 import BeautifulSoup
import requests
r = requests.get("https://ballotpedia.org/Alabama_Supreme_Court")
soup = BeautifulSoup(r.content, 'html.parser')
print([item.text for item in soup.select(
"table.wikitable.sortable.jquery-tablesorter a")])
现在,您必须阅读有关CSS-Selection 的内容
输出:
['Brad Mendheim', 'Kelli Wise', 'Michael Bolin', 'William Sellers', 'Sarah Stewart', 'Greg Shaw', 'Tommy Bryan', 'Jay Mitchell', 'Tom Parker']
推荐阅读
- android - 当 DAO 方法需要更改参数时,如何使用 LiveData?
- firebase - ServiceWorker:离线插件和 Firebase
- javascript - 使用 Javascript 在 NodeJS 服务器上打开一个新页面并添加一些东西?
- python - 如何在不更改列表长度的情况下删除列表中多次出现的元素(无集合)
- sql - 合并两张表,旧列的新列总和
- git - 如果当前分支尚未完成,如何将其他分支的更改合并到当前分支中?
- c# - 构建解决方案时VS2017项目回退框架错误
- android - 领域查询更改的侦听器
- python - 使用python 2.7在端口80上发送数据时如何解决错误10045
- algorithm - 使用行/列跨度布置网格(类似 html 的表格)的算法