La reducción a lo que estoy raspado de un sitio web utilizando Python

Anterthorp:

Estoy tratando de practicar mi pitón raspado de sitios web, pero estoy teniendo problemas para reducirla a un tamaño razonable sin pitón no reconocer lo que estoy pidiendo. Por ejemplo, aquí está mi código:

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)

Estoy tratando de raspar los nombres de los jueces de la Corte Suprema del Estado de Alabama, pero con este código, consigo demasiada información. He tratado de cosas, tales como (en la fila 6)

y = soup.find('table',{'class':'wikitable sortable'})`

pero me sale un mensaje diciendo que la búsqueda no encuentra resultados.

Aquí está una imagen de la inspección de la página web. Mi objetivo es conseguir la culata en T a la obra en mi código, pero estoy fallando!

¿Cómo puedo especificar al pitón que quiero sólo los nombres de los jueces?

¡Muchas gracias!

aԋɱҽԃ aмеяicai:

Simplemente, voy a hacerlo de esta manera.

import pandas as pd

df = pd.read_html("https://ballotpedia.org/Alabama_Supreme_Court")[2]["Judge"]

print(df.to_list())

Output:

['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.

there's difference between find which will return only the first element but find_all will return a list of elements. Check the Documentation.

import directly from bs4 import BeautifulSoup instead of import bs4 as it's the The DRY Principle of Python.

Leave bs4 to handle the content as it's one of it's tasks in the back-ground. so instead of r.text use r.content

Now, we will deep into the HTML to select it:

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")])

Now, you have to read about CSS-Selection

Output:

['Brad Mendheim', 'Kelli Wise', 'Michael Bolin', 'William Sellers', 'Sarah Stewart', 'Greg Shaw', 'Tommy Bryan', 'Jay Mitchell', 'Tom Parker']

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=365662&siteId=1
Recomendado
Clasificación