Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

pandas - How do I get my parsed data to fall under certain columns? [python]

Here is my code. I've scraped the data from website but it just returns to me one long list.

How do I manipulate the data to fall under the headings? I'm getting the current error message:

ValueError: 8 columns passed, passed data had 2648 columns.

Any help is greatly appreciated.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import pandas as pd
from pandas import DataFrame
import html5lib

url = "https://www.loudnumber.com/screeners/cashflow"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)


time.sleep(5)

html = driver.page_source
soup = BeautifulSoup(html,'html.parser')
table = soup.find('table')
l = []
for tr in table:
    td = table.find_all('td') #cols
    rows = [table.text.strip() for tr in td if tr.text.strip()] 
    if rows:
        l.append(rows)
                            

driver.quit()


df = pd.DataFrame(list(l), columns=["Ticker","Company","Industry","Current Price"
                                    ,"Instrinsic Value","IV to CP ratio",
                                    "Dividend","Dividend Yield"])

print(df)
question from:https://stackoverflow.com/questions/65872117/how-do-i-get-my-parsed-data-to-fall-under-certain-columns-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Lots of things are totally wrong here, so fixed your code.

Note: Put selenium drivers in python folder to make this code work or give its path. refer this selenium - chromedriver executable needs to be in PATH

Here is complete code:

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time

url = "https://www.loudnumber.com/screeners/cashflow"
driver = webdriver.Chrome()
driver.get(url)
time.sleep(3)

html = driver.page_source
soup = BeautifulSoup(html,'html.parser')
table = soup.find("div", class_="table-responsive").tbody.find_all("tr")
l = []

for tr in table:
    td = tr.find_all('td') #cols
    rows = [i.text.strip() for i in td if i.text.strip()]
    l.append(rows)

driver.quit()

df = pd.DataFrame(list(l), columns=["Ticker","Company","Industry","Current Price" ,"Instrinsic Value","IV to CP ratio","Dividend","Dividend Yield"])
print(df)

Here is the result: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...