HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Proxy evasion browser in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pythonproxyevasionbrowser

Problem

This code works. It is designed to retrieve website data directly, parse it, and open it as normal. I will develop it further later. Right now I want to know if there is any way to improve efficiency, such as not using a file etc. I would also like to know if my code could be made more pythonic (this would include PEP standards), or incidences where concatenation would be acceptable to readability. Here is the code:

import urllib.request
from tkinter import *
import webbrowser
import os

proxy_handler = urllib.request.ProxyHandler(proxies=None)
opener = urllib.request.build_opener(proxy_handler)

def navigate(query):
    response = opener.open(query)
    html = response.read()
    return html

def parse(junk):
    ugly = str(junk)[2:-1]
    lines = ugly.split('\\n')
    return lines

while True:
    url = input("Path: ")
    dump = navigate(url)

    content = parse(dump)
    with open('cache.html', 'w') as f:
        for line in content:
            f.write(line)

    webbrowser.open_new_tab(os.path.dirname(os.path.abspath(__file__))+'/cache.html')

Solution


  1. Make sure your variable names are both short and to the point, using a parameter called junk is pointless. Also, this could cause conflicts if you ever write other code and import this.



def parse(junk):
    ugly = str(junk)[2:-1]
    lines = ugly.split('\\n')
    return lines


  1. urllib is outdated. Unless there is a specific reason you need to use urllib, you should use urllib2. Urllib2 is faster and more simple.



import urllib.request

  1. This part looks a bit messy:


webbrowser.open_new_tab(os.path.dirname(os.path.abspath(__file__))+'/cache.html')
Perhaps separate into multiple lines for readability.

Code Snippets

def parse(junk):
    ugly = str(junk)[2:-1]
    lines = ugly.split('\\n')
    return lines

Context

StackExchange Code Review Q#41970, answer score: 2

Revisions (0)

No revisions yet.