python – Why does my code stop after processing one html?-ThrowExceptions

Exception or error:

I have created a code, but it stops after one html is processed. Am I overlooking something?
I would appreciate the help!

import textwrap
import os
import pandas as pd
import textwrap
import os
from bs4 import BeautifulSoup

directory ='C:/Research syntheses - Meta analysis/SeekingAlpha/Tests'
for filename in os.listdir(directory):
    if filename.endswith('.html'):
        fname = os.path.join(directory,filename)
        with open(fname,errors='ignore') as f:
            soup = BeautifulSoup(f.read(),'html.parser')

print('{:<30} {:<70}'.format('Name', 'Answer'))
print('-' * 101)
for answer in soup.find('div',class_='content_part hid', id='article_qanda').find_all('strong'):
    txt = answer.get_text(strip=True)

    s = answer.find_next_sibling()
    while s:
        if s.name == 'strong' or s.find_all('strong'):
            break
        if s.name == 'p':
            txt += ' ' + s.get_text(strip=True)
        s = s.find_next_sibling()

    txt = ('\n' + ' '*31).join(textwrap.wrap(txt))
    print('{:<30} {:<70}'.format('exec', txt))
How to solve:

Thanks to ekhumoro, the answer is:

import textwrap
import os
import pandas as pd
import textwrap
import os
from bs4 import BeautifulSoup

directory ='C:/Research syntheses - Meta analysis/SeekingAlpha/Tests'
for filename in os.listdir(directory):
    if filename.endswith('.html'):
        fname = os.path.join(directory,filename)
        with open(fname,errors='ignore') as f:
            soup = BeautifulSoup(f.read(),'html.parser')

        print('{:<30} {:<70}'.format('Name', 'Answer'))
        print('-' * 101)
        for answer in soup.find('div',class_='content_part hid', id='article_qanda').find_all('strong'):
            txt = answer.get_text(strip=True)

            s = answer.find_next_sibling()
            while s:
                if s.name == 'strong' or s.find_all('strong'):
                    break
                if s.name == 'p':
                    txt += ' ' + s.get_text(strip=True)
                s = s.find_next_sibling()

            txt = ('\n' + ' '*31).join(textwrap.wrap(txt))
            print('{:<30} {:<70}'.format('exec', txt))

Leave a Reply

Your email address will not be published. Required fields are marked *