Denis
Denis

Reputation: 99

Python: how to save a webpage (as html file) from the link in the Email

I get every day one specific email with a link in it. Then I klick the link which opens a browser, and then I save the webpage as a html file. That is what I have to do every day. Until now I do it manually, but I guess that it is a way to do it with python. I only know for now how to save an attachment from an email using python. But I don't know what to do with a link. Have somebody some experience with it?

Thanks in advance

Upvotes: 2

Views: 1545

Answers (1)

Andrew F
Andrew F

Reputation: 2950

You can do this with the requests package. Assuming you've already extracted the link as a string

link = 'https://...'

from requests import get
resp = get(link)
with open('todays-file.html', 'wb') as fOut:
    fOut.write(resp.content)

You will probably want to add handling if the link is bad (i.e. does not return a 20x status code), but that's the general idea.

Upvotes: 2

Related Questions