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

Creating a dictionary of multiple results

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

Problem

I am creating list of dictionary from two different results. I appreciate any improvement suggested.

redditFile = urllib2.urlopen("http://www.bing.com/images?q="+word)
redditHtml = redditFile.read()
redditFile.close()
soup = BeautifulSoup(redditHtml)

productDivs = soup.findAll('div', attrs={'class' : 'dg_u'})
for div in productDivs:
    #print div.find('a')['t1']
    #print div.find('img')['src2']
    img_result = {}
    img_result['link'] = div.find('img')['src2']
    img_result['title'] = div.find('a')['t1']
    image_result_final.append(img_result)

final_result['img'] = image_result_final

print "*"*25

redditFile = urllib2.urlopen("http://www.bing.com/videos?q="+word)
redditHtml = redditFile.read()
#redditHtml = open('source.txt').read()
redditFile.close()
soup = BeautifulSoup(redditHtml)
productDivs = soup.findAll('div', attrs={'class' : 'dg_u'})
for div in productDivs:
    if div.find('div', {"class":"vthumb", 'smturl': True}) is not None:
        #print div.find('div', {"class":"vthumb", 'smturl': True})['smturl']
        #print div.find("div", {"class":"tl fade"}).text
        video_result = {}
        video_result['link'] = div.find('div', {"class":"vthumb", 'smturl': True})['smturl']
        video_result['title'] = div.find("div", {"class":"tl fade"}).text
        video_result_final.append(video_result)

final_result['video'] = video_result_final
import json
print json.dumps(final_result)

Solution

Since I'm not really a web guy, this is going to be a style review, like my other reviews.

  • Why are you mixing camelCase and underscore_case together when naming variables? Variables and functions should be underscore_case, and classes should be PascalCase.



  • This seems like something that could be separated into functions, which allows for easier reusability. See if you can take parts of the code and separate them into their relevant parts.



  • I noticed that the 2nd-to-last line of this Python script is import json. You shouldn't be importing libraries after code has run. You should put imports/includes at the top of your script.



  • Why are you doing this? print ""25. It doesn't really seem like it's needed. Same with print json.dumps(final_result). That should be a return inside of a function.



  • You shouldn't be using file.close(). The reason for this is that the file isn't explicitly closed. You should be using a with..as context manager, like this.



with open("path/to/myfile.ext", "mode") as file_name:
     # Blah blah blah code goes here


  • Finally, if you aren't using parts of the code, don't comment them out. Just remove them completely. Commented out lines of code are an eyesore to look at.



That's about all I have. If there's anything else you might want me to cover, just mention it in the comments, and I'll see what I can do. Hope this helps!

Code Snippets

with open("path/to/myfile.ext", "mode") as file_name:
     # Blah blah blah code goes here

Context

StackExchange Code Review Q#93179, answer score: 2

Revisions (0)

No revisions yet.