我正试图从新汉普郡国务卿的网站上获取登记选民的信息。到目前为止,我已经能够通过以下代码获得美丽汤中的网站文本:
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from openpyxl import Workbook
import getpass
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = urlopen('http://sos.nh.gov/NamesHistory.aspx')
html = BeautifulSoup(url, 'html.parser')
html.find('table', attrs={'class':'table-border2-black'}).get_text
然而,我的问题是,我如何才能将该表中的文本放入一个可用的数据框中,就像出现在网站(http://sos.nh.gov/NamesHistory.aspx)上的数据框一样?我的问题是不同的,因为这个网站与以前的网站不同。
发布于 2018-07-20 09:31:02
您需要使用以下命令通过csv
文件转换报废数据。
import csv
with open ('filename.cv','wb') as file:
writer=csv.writer(file)
for row in course_list:
writer.writerow(row)
你也可以在这里看到它,writing and saving CSV file from scraping data using python and Beautifulsoup4。
之后,您需要访问csv文件并将数据转换为数据帧以进行进一步处理。如果你不知道如何做到这一点,请阅读pandas文档,从这里开始:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.from_csv.html
https://stackoverflow.com/questions/51433739
复制相似问题