Last active
January 17, 2017 23:07
-
-
Save hustshawn/b41840a3584c2f127116f969cbac34d9 to your computer and use it in GitHub Desktop.
An online scrapying img demo with selenium and beautifulsoup. Sources: "http://www.chrisburkard.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
import os | |
import requests | |
import shutil | |
import time | |
from bs4 import BeautifulSoup | |
from selenium import webdriver | |
url = "http://www.chrisburkard.com/" | |
# web_r = requests.get(url) | |
# web_soup = BeautifulSoup(web_r.content, "lxml") | |
# print(web_soup.findAll('img')) | |
driver = webdriver.Firefox() | |
driver.get(url) | |
iteations = 0 | |
while iteations <10: | |
html = driver.execute_script("return document.documentElement.outerHTML") | |
sel_soup = BeautifulSoup(html, 'lxml') | |
print(len(sel_soup.find_all('img'))) | |
images = [] | |
for i in sel_soup.find_all('img'): | |
src = i['src'] | |
images.append(src) | |
print(images) | |
current_path = os.getcwd() | |
for img in images: | |
try: | |
file_name = os.path.basename(img) | |
img_r = requests.get(img, stream=True) | |
new_path = os.path.join(current_path, 'images', file_name) | |
with open(new_path, 'wb') as output_file: | |
shutil.copyfileobj(img_r.raw, output_file) | |
del img_r | |
except: | |
pass | |
iteations += 1 | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment