Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Looking at the site, i suppose not to see an error because each local language(Yoruba) as it Meaning and Translation, and there are 220 local language(Yoruba).

from bs4 import BeautifulSoup
import requests
import pandas as pd
import re

res = requests.get('http://yoruba.unl.edu/yoruba.php-text=1a&view=0&uni=0&l=1.htm')
soup = BeautifulSoup(res.content,'html.parser')

edu = {'Yoruba':[],'Translation':[],'Meaning':[]}
    # first loop
for br in soup.select('p > br:nth-of-type(1)'):
    text = br.previous_sibling.strip()
    edu['Yoruba'].append(text)
    # second loop
for br in soup.select('p > br:nth-of-type(2)'):
    text = br.previous_sibling
    if isinstance(text, str):
        edu['Translation'].append(text.strip())
    # third loop
for br in soup.select('p > br:nth-of-type(3)'):
    text = br.previous_sibling
    if isinstance(text, str):
        edu['Meaning'].append(re.sub(r'[()]','',str(text.strip())))

df7 = pd.DataFrame(edu)

Error

ValueError: arrays must all be same length

See Question&Answers more detail:os

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

1 Answer

Since each of the three keys has different length, I guess the best way to address it is to pad the short keys to the length of the longest key (220, in this case). To do that add the following right before creating your dataframe:

length = max(len(edu['Meaning']),len(edu['Translation']),len(edu['Yoruba'])) #in case you don't know, find the length of the longest key
for k in edu:
    for i in range(length-len(edu[k])):
        edu[k].append("NA") # this is where the padding is; you can replacing NA with anything else, obviously

df7 = pd.DataFrame.from_dict(edu) #since edu is a dictionary, I would use this method
df7

Let me know if that works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...