assign lines from txt file to html files regex bs4

試著忘記壹切 提交于 2020-02-05 05:19:25

问题


i need to assign a line from a text file containing 7 lines every line with a url, these urls should be replacing href url in 7 html files

the issue is that i had the same value or the same url in all html files on all seven html files here is my code:

regex = re.compile("https\:\/\/(.*)\n")
with open("urls.txt") as f:
        for line in f:
            result = regex.search(line)
            for filename in glob.glob('/htmlz/*.html'):
                with open(filename, "r") as html_file:
                    soup = BeautifulSoup(html_file,'html.parser')
                    for anchor in soup.findAll("a", attrs={ "class" : "link" }):
                        anchor['href'] = result.group(0)
                        with open(filename, "w") as htmlfile2:
                            htmlfile2.write(str(soup))

来源:https://stackoverflow.com/questions/59599669/assign-lines-from-txt-file-to-html-files-regex-bs4

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