beautifulsoup parse every html files in a folder webscrapping [closed]

房东的猫 提交于 2019-12-13 07:43:39

问题


My task is to read every html file from a directory. Conditions are to find whether each file contains tags

(1) <strong>OO</strong>  
(2) <strong>QQ</strong>

Then


回答1:


Your write function is nested in the for loop, that's why you write multiple lines to your index.txt, just move the write out of the loop and put all your parti text to a variable parti_names like this:

participants = soup.find(find_participant)
parti_names = ""
for parti in participants.find_next_siblings("p"):
    if parti.find("strong", text=re.compile(r"(Operator)")):
        break
    parti_names += parti.get_text(strip=True)+","
    print parti.get_text(strip=True)

indexFile = open('index.txt', 'a+')
indexFile.write(filename + ', ' + title.get_text(strip=True) + ticker.get_text(strip=True) + ', ' + d_date.get_text(strip=True) + ', ' + parti_names + '\n' )
indexFile.close()

Update:

You can work with basename to get the file name:

from os.path import basename

# you can call it directly with basename
print(basename("C:/Users/.../output/100107-.html"))

Output:

100107-.html


来源:https://stackoverflow.com/questions/44232216/beautifulsoup-parse-every-html-files-in-a-folder-webscrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!