forked from jeanphix/Ghost.py
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve error with the selective Download
Updates in the documentation New Examples
- Loading branch information
1 parent
c70311a
commit 210f909
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from ghost import Ghost | ||
|
||
# Here we create a ghost instance with cache in /tmp/ghost.py and a max size of 10 MB. | ||
# We also indicates that the cache folder would be shared between tabs | ||
gh = Ghost(cache_dir="/tmp/ghost.py", cache_size=10, share_cache=True) | ||
gpage, name = gh.create_page(download_images=True, prevent_download=["css"]) | ||
|
||
# Open google | ||
gpage.open("http://www.google.com") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from ghost import Ghost | ||
|
||
url = "http://news.ycombinator.com/" | ||
# We enable the cache and set the maximun size to 10 MB | ||
# We don't want to load images and load css or js files | ||
gh = Ghost(cache_size=10) | ||
|
||
# We create a new page | ||
page, page_name = gh.create_page(download_images=False, | ||
prevent_download=["css", "js"]) | ||
|
||
# wait_onload_event will tell to Ghost to leave the open method | ||
# when the On Ready event on the web page has been fired | ||
page_resource = page.open(url, wait_onload_event=False) | ||
|
||
# We retrive the links from the web page | ||
links = page.evaluate(""" | ||
var links = document.querySelectorAll("a"); | ||
var listRet = []; | ||
for (var i=0; i<links.length; i++){ | ||
listRet.push(links[i].href); | ||
} | ||
listRet; | ||
""") | ||
# Print the links | ||
for l in links: | ||
print l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from ghost import Ghost | ||
|
||
# Creates a Ghost instance telling to share cache and don't share cookies | ||
gh = Ghost(share_cache=True, share_cookies=False) | ||
page1, name = gh.create_page() | ||
page2, name = gh.create_page() | ||
|
||
# Open google | ||
page1.open("http://www.google.com") | ||
|
||
# Open Hacker News | ||
page2.open("http://news.ycombinator.com/") | ||
|
||
# Saves an image of the screen 1 | ||
page1.capture_to("/tmp/tab1.png") | ||
print "open /tmp/tab1.png" | ||
|
||
# Saves an image of the screen 2 | ||
page2.capture_to("/tmp/tab2.png") | ||
print "open /tmp/tab2.png" | ||
|
||
# Then we remove the tabs from ghost instance | ||
gh.remove_page(page1) | ||
gh.remove_page(page2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from ghost import Ghost | ||
import time | ||
|
||
# Creates a Ghost instance telling to share cache and don't share cookies | ||
gh = Ghost(share_cache=True, share_cookies=False) | ||
page1, name = gh.create_page() | ||
page2, name = gh.create_page() | ||
|
||
# Open google | ||
page1.open("http://www.google.com", wait_for_loading=False) | ||
|
||
# Open Hacker News | ||
page2.open("http://news.ycombinator.com/", wait_for_loading=False) | ||
|
||
# At this point ghost it's rendering the two pages at the same time. Now we | ||
# need to check if the pages are loaded. | ||
while page1.get_loaded_page() is None: | ||
page_resource = page1.get_loaded_page() | ||
gh.process_events() | ||
time.sleep(0.01) | ||
|
||
# Saves an image of the screen 1 | ||
page1.capture_to("/tmp/tab1.png") | ||
print "open /tmp/tab1.png" | ||
|
||
|
||
#Or we can use wait_for_page_loaded() instead | ||
page2.wait_for_page_loaded() | ||
|
||
# Saves an image of the screen 2 | ||
page2.capture_to("/tmp/tab2.png") | ||
print "open /tmp/tab2.png" | ||
|
||
# Then we remove the tabs from ghost instance | ||
gh.remove_page(page1) | ||
gh.remove_page(page2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from ghost import Ghost | ||
|
||
# Creates a Ghost instance telling to don't download css files and images | ||
gh = Ghost() | ||
gpage, name = gh.create_page(download_images=True, prevent_download=["css"]) | ||
|
||
# Open google | ||
gpage.open("http://www.google.com") | ||
|
||
# Saves an image of the screen | ||
gpage.capture_to("/tmp/selective_download.png") | ||
|