使用 python 缩小我从网站上抓取的内容
Narrowing down what I am scraping from a website using 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']
Now Moving back to the original issue
to solve it as I personally love to fix the real issue without navigating to alternative solutions.
find
之间存在差异,后者 return 仅第一个 element
而 find_all
将 return list
of [=19] =].检查 Documentation.
直接导入 from bs4 import BeautifulSoup
而不是 import bs4
因为它是 Python.
的 The DRY Principle
让 bs4
处理内容,因为它是后台任务之一。所以不用 r.text
使用 r.content
现在,我们就深入HTML
到select吧:
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']
我正在尝试练习我的 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 在我的代码中工作,但失败了!
非常感谢!
简单的,我就这样吧
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']
Now Moving back to the original
issue
to solve it as I personally love to fix the real issue without navigating to alternative solutions.
find
之间存在差异,后者 return 仅第一个 element
而 find_all
将 return list
of [=19] =].检查 Documentation.
直接导入 from bs4 import BeautifulSoup
而不是 import bs4
因为它是 Python.
让 bs4
处理内容,因为它是后台任务之一。所以不用 r.text
使用 r.content
现在,我们就深入HTML
到select吧:
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']