Last active
September 13, 2018 16:36
-
-
Save ericbarnhill/2725c748a25cb8e8fc24a18442147bfc to your computer and use it in GitHub Desktop.
This file contains hidden or 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 socks | |
import socket | |
import stem.process | |
def scrape_with_tor(): | |
SOCKS_PORT=9050# You can change the port number | |
tor_process = stem.process.launch_tor_with_config( | |
config = { | |
'SocksPort': str(SOCKS_PORT), | |
}, | |
) | |
socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, | |
addr="127.0.0.1", | |
port=SOCKS_PORT) | |
socket.socket = socks.socksocket | |
# ... | |
with request.urlopen(url_request) as response: | |
try: | |
page = response.read() | |
except (http.client.IncompleteRead) as e: | |
page = e.partial | |
# ... | |
tor_process.kill() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment