简体   繁体   中英

How to search for a particular data in the url using python

Hello i'm working in something and i have got stucked in a script whose first method takes a string argument and do a google search on it the it moves into in the second method which is takes the result of the website

for ex:

from googlesearch import search

class online():
      def __init__(self):
            self.search = ""
      def get_url(self):
           for i in search(self.search,num_resuls=10):
                     return i

      def dump_data(self):
            pass 

so let's say that the search = how to make money?

it will give me the link to an article

so how can i dump that data which answer this question in the url?

and the search() method from googlesearch module only returns 1 url how can i fix that?

If all you want is to iterate over the top 10 search results, then you can simply return the generator that search() returns. Something like this:

from googlesearch import search


class Google(object):
    def __init__(self, terms):
        self.terms = terms

    def get_urls(self):
        return search(self.terms, num_results=10)

    def dump_data(self):
        pass


for url in Google("how to make money?").get_urls():
    print(url)

Note it's a bad idea to name your class attributes the same name (eg "search") as the function you imported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM