Python script that runs an iterating google search and prints top results and links

て烟熏妆下的殇ゞ 提交于 2019-12-11 10:27:29

问题


I want to write a python script that will pull the title and url of the top three links of successive google searches. For example, I want to be able to google "3 mile run", "4 mile run", and "5 mile run" and get the top three links from each.

I tried modifying some code I found on here that allows you to print the top results of one google search that the user inputs.

I put the entire block in a for loop and made the query a specific search that increases with x.

import urllib
import json as m_json

for x in range(3, 5): 
    query = 'x mile run'
    query = urllib.urlencode ( { 'q' : query } )
    response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
    json = m_json.loads ( response )
    results = json [ 'responseData' ] [ 'results' ]
    for result in results:
        title = result['title']
        url = result['url']   # was URL in the original and that threw a name error exception
        print ( title + '; ' + url )

I keep getting unexpected indent errors though, and I was wondering if anyone could help.


回答1:


Indent errors are only caused by indentation problems.

It seems obvious, but check your code does not have, say a mixture of tabs and spaces for indenting. Your editor might show 4x spaces the same size as one tab. But python does not see it this way.

In the Vi (or Vim) editor, a command to replace the tabs with spaces would be:

:1,$s/[CTRL-v][TAB]/    /g

Failing that you can manually remove and replace them. With spaces or tabs, but I suggest spaces.

Copying and pasting your code, it works for me, so probably the act of putting it here has normalised the mix of spaces and tabs.




回答2:


Using ViM, you can fix it very easily:

:set et
:retab
:w

It may help you:

  • :et (:expandtabs) tells ViM to prefer spaces over tabs;
  • :retab tells ViM to rebuild all tabs as its preferences (spaces);
  • finally, :w saves your file changes.

If the tabs expand to odd count of spaces, undo the retab, and try:

:set tabstop=4

Then :retab and :w.

You can tell ViM always to deal with Python files this way. Edit ~/.vim/ftdetect/python.vim (if it doesn’t exists, create it):

au BufNewFile,BufRead *.py  set filetype=python
au BufNewFile,BufRead *.pyw set filetype=python
au FileType python set et
au FileType python set tabstop=4


来源:https://stackoverflow.com/questions/35023259/python-script-that-runs-an-iterating-google-search-and-prints-top-results-and-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!