I'm writing new code and having problem getting desired output. The code reads an html file and finds tags. it outputs the url only. I insert additional code to complete the link. I'm trying to insert the url two times within the string.

####### Parse for <a> tags and save ############
with open("page1.html", 'r') as htmlb:
    soup2 = BeautifulSoup(htmlb, 'lxml')
links = []
for link in soup2.findAll('a', attrs={'href':   re.compile("^https://")}):
    links.append('<a href="'+link.get('href')+'">'"{link}"'</a><br>')

time.sleep(.1)

with  open("page-2.html", 'w') as html:
    html.write('{links}\n'.format(links=links))

2 Answers

0
dejdej On Best Solutions

This should give you the desired html output file:

import re
from bs4 import BeautifulSoup
import html 

with open("page1.html", 'r') as htmlb:
    soup2 = BeautifulSoup(htmlb, 'lxml')



with open("page2.html", 'w') as h:
    for link in soup2.find_all('a'):
       h.write("<a href=\"{}\">{}</a><br>".format(link.get('href'),link.get('href')))    
0
Community On

This gives me want I want I guess, but not exactly. I would rather see it written out "https://whatever.com/text/text/" than to see "whatever.com/text/text"

####### Parse for <a> tags and save ############
with open("page1.html", 'r') as htmlb:
    soup2 = BeautifulSoup(htmlb, 'lxml')
links = []
for link in soup2.findAll('a', attrs={'href':   re.compile("^https://")}):
links.append('{0}</a><br>'.format(link,link))

with  open("page-2.html", 'w') as html:
    html.write('{links}\n'.format(links=links))