Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser simulation - Python

I need to access a few HTML pages through a Python script, problem is that I need COOKIE functionality, therefore a simple urllib HTTP request won't work.

Any ideas?

like image 476
RadiantHex Avatar asked Apr 02 '10 16:04

RadiantHex


People also ask

Can Python be used for simulation?

In this tutorial, you'll learn how to use Python's simpy framework to create virtual simulations that will help you solve problems like these. In this tutorial, you'll learn how to: Use a simulation to model a real-world process. Create a step-by-step algorithm to approximate a complex system.

Can Python run in a browser?

Python might be the most popular programming language in the world, but unlike other frontrunner JavaScript, you can't run Python code in the browser.

Is PyScript production ready?

Pyscript is in alpha and is not production-ready, but Wang did feel it was ready enough to demo during his keynote at PyCon 2022.


2 Answers

Why don't you try Dryscrape for this:

Import dryscrape as d
d.start_xvfb()
Br = d.Session()
Br.visit('http://URL.COM')
#open webpage
Br.at_xpath('//*[@id = "email"]').set('[email protected]')
#finding input by id
Br.at_xpath('//*[@id = "pass"]').set('pasword') 
Br.at_xpath('//*[@id = "submit_button"]').click()
#put id of submit button and click it

You don't need cookie lib to store cookies just install Dryscrape and do it in your style

like image 91
Harry1992 Avatar answered Oct 03 '22 17:10

Harry1992


check out Mechanize. "Stateful programmatic web browsing in Python".
It handles cookies automagically.

import mechanize

br = mechanize.Browser()
resp = br.open("http://www.mysitewithcookies.com/")
print resp.info()  # headers
print resp.read()  # content

mechanize also exposes the urllib2 API, with cookie handling enabled by default.

like image 26
Corey Goldberg Avatar answered Oct 03 '22 17:10

Corey Goldberg