Babapt
Babapt

Reputation: 13

Open html page in IE with urllib

I have issues for opening an url using python and urllib in IE, I don't know how to specify to open the url in IE and not Chrome. I can specify it when using webbrowser lib or subprocess, but what I need is to get the HTML code of the desired page without opening. Here what I tried (I know that for subprocess CREATE_NO_WINDOW is for console):

url = "myurl.com"
ie = webbrowser.get("C:\\program files\\internet explorer\\iexplore.exe") #specify browser for webbrowser
ie.open(url)#correctly open it in IE but can t get the HTML code with this lib
        
        subprocess.Popen(
            '"C:\\Program Files\\Internet Explorer\\iexplore.exe" %s' 
            % url, stdout = subprocess.PIPE, creationflags = subprocess.CREATE_NO_WINDOW)
            # open in IE but open the browser and show the web page

        html = urllib.request.urlopen(url) #always open in chrome

If you have any clews for me to solve this problem thank you :)!

Upvotes: 0

Views: 144

Answers (1)

Xim_Vyge
Xim_Vyge

Reputation: 11

So I have this code which gets the html code from site without opening a browser and stores it as XPath data in a tree:

import requests
from lxml import html

page = requests.get(link)
tree = html.fromstring(page.content)

subcode = tree.xpath(x-path)
print("subcodes:\n" + "\n".join(subcode))

This uses the requests library and the lxml library. Both can be installed using pip.

This site might also help you.

Upvotes: 1

Related Questions