Python: Как я могу поместить информацию, которую я собираю, в Excel do c или большой двоичный объект, чтобы я мог сравнивать их при выполнении моего кода? - PullRequest
0 голосов
/ 25 марта 2020

Я пытаюсь собрать информацию в верховных судах штата, чтобы проверить, когда она изменится. Я могу успешно отсканировать и распечатать информацию, но я изо всех сил пытаюсь найти способ передать ее в Excel do c или в другую форму хранилища больших двоичных объектов. Вот мой текущий python код:

import requests
from bs4 import BeautifulSoup
import pandas as pd

list = ['https://ballotpedia.org/Alabama_Supreme_Court', 
'https://ballotpedia.org/Alaska_Supreme_Court', 'https://ballotpedia.org/Arizona_Supreme_Court', 
'https://ballotpedia.org/Arkansas_Supreme_Court', 'https://ballotpedia.org/California_Supreme_Court', 
'https://ballotpedia.org/Colorado_Supreme_Court', 
'https://ballotpedia.org/Connecticut_Supreme_Court', 
'https://ballotpedia.org/Delaware_Supreme_Court', 'https://ballotpedia.org/Florida_Supreme_Court']
for page in list:
    r = requests.get(page)
    soup = BeautifulSoup(r.content, 'html.parser')
    print([item.text for item in soup.select("table.wikitable.sortable.jquery-tablesorter a")])'

Как я могу получить это в Excel do c или хранилище BLOB-объектов и обратиться к нему позже, чтобы проверить, изменилась ли информация. Спасибо!

1 Ответ

1 голос
/ 26 марта 2020

Сделано несколько корректировок:

import requests
from bs4 import BeautifulSoup
import pandas as pd

list = ['https://ballotpedia.org/Alabama_Supreme_Court', 
'https://ballotpedia.org/Alaska_Supreme_Court', 'https://ballotpedia.org/Arizona_Supreme_Court', 
'https://ballotpedia.org/Arkansas_Supreme_Court', 'https://ballotpedia.org/California_Supreme_Court', 
'https://ballotpedia.org/Colorado_Supreme_Court', 
'https://ballotpedia.org/Connecticut_Supreme_Court', 
'https://ballotpedia.org/Delaware_Supreme_Court', 'https://ballotpedia.org/Florida_Supreme_Court']

temp_dict = {} #create empty dictionary

for page in list:
    r = requests.get(page)
    soup = BeautifulSoup(r.content, 'html.parser')

    temp_dict[page.split('/')[-1]] = [item.text for item in soup.select("table.wikitable.sortable.jquery-tablesorter a")] #populate dictionary with state as key and the info as the value. 

# The next line does the following: create dataframe from dictionary,
# orient as 'index' (this handles different lengths of arrays) 
# transpose it back so state supreme courts are column headers

df = pd.DataFrame.from_dict(temp_dict, orient='index').transpose() 
df.to_csv('State_Supreme_Court_Info.csv') #saves as csv
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...