0

so I've downloaded the HTML of a web page. I'm supposed to extract all of the links from the HTML and output them. Here is my code

f = open('html.py','r')
heb = f.readlines()
arry = []
if 'href' in heb:
    arry = arry.append(href)

    print(arry)

I'm trying to make a list of the links and output it, but honestly I'm pretty lost. Can someone point me in the right direction? I was thinking regex probably is the way to go thanks

2

1 Answer 1

3

You can use Beautiful Soup (which you'll need to install, e.g. with pip install BeautifulSoup4):

import bs4

with open("my-file.html") as f:
    soup = bs4.BeautifulSoup(f)

links = [link['href'] for link in soup('a') if 'href' in link.attrs]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.