Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การค้นหา Google ด้วยโค้ด Python


ในบทความนี้ เราจะลองทำ Google Search โดยใช้โค้ด Python นี่เป็นประโยชน์ในกรณีที่คุณทำงานในโปรเจกต์ Python และต้องเข้าถึงข้อมูลจากเว็บ และผลลัพธ์การค้นหา (จากเว็บ) จะถูกนำไปใช้ภายในโปรเจกต์ของคุณ

ข้อกำหนดก่อนหน้า –

  • คุณต้องมี Python ติดตั้งในระบบของคุณ
  • ติดตั้งโมดูล google คุณสามารถใช้ pip ติดตั้งโมดูล google ได้ดังนี้ −
C:\Users\rajesh>python -m pip install google Collecting google Downloading https://files.pythonhosted.org/packages/c8/b1/887e715b39ea7d413a06565713c5ea0e3132156bd6fc2d8b165cee3e559c/google-2.0.1.tar.gz Requirement already satisfied: beautifulsoup4 in c:\python\python361\lib\site-packages (from google) (4.6.0) Installing collected packages: google Running setup.py install for google ... done Successfully installed google-2.0.1

หากข้อกำหนดก่อนหน้าทั้งหมดเสร็จแล้ว คุณสามารถเขียนโค้ดเพื่อทำ Google Search ด้วย Python ได้

ด้านล่างคือโปรแกรมที่ผู้ใช้ต้องการค้นหาคีย์เวิร์ดเฉพาะ (เช่น: "AI in python" หรือ "Tutorialspoint") และต้องการลิงก์ทั้งหมด (สมมติ 10 ผลลัพธ์แรกจาก Google Search) เพื่อนำไปใช้ในโปรเจกต์ Python ของเขา

# Performing google search using Python code class Gsearch_python:     def __init__(self,name_search):         self.name = name_search     def Gsearch(self):         count = 0         try :             from googlesearch import search         except ImportError:             print("No Module named 'google' Found")         for i in search(query=self.name,tld='co.in',lang='en',num=10,stop=1,pause=2):             count += 1             print (count)             print(i + '\n') if __name__=='__main__':     gs = Gsearch_python("Tutorialspoint Python")     gs.Gsearch()

ผลลัพธ์

1 https://www.tutorialspoint.com/python/ 2 https://www.tutorialspoint.com/python3/ 3 https://www.tutorialspoint.com/python_online_training/index.asp 4 https://www.tutorialspoint.com/python/python_overview.htm 5 https://www.tutorialspoint.com/python/python_loops.htm 6 https://www.tutorialspoint.com/python/python_pdf_version.htm 7 https://www.tutorialspoint.com/python/python_basic_syntax.htm 8 https://www.tutorialspoint.com/tutorialslibrary.htm 9 https://www.tutorialspoint.com/ 10 https://www.tutorialspoint.com/django/ 11 https://www.tutorialspoint.com/numpy 12 https://www.quora.com/I-have-learned-Python-from-Tutorials-Point-What-should-I-do-to-learn-more-topics-so-that-I-can-have-more-advantages-on-my-interviews 13 https://www.pdfdrive.com/python-tutorial-tutorials-point-e10195863.html

ผลลัพธ์ที่คล้ายกันเราจะได้เมื่อลองค้นหาผ่านเบราว์เซอร์ −

การค้นหา Google ด้วยโค้ด Python

ในกรณีที่เราต้องการผลลัพธ์การค้นหาคิวรีโดยตรงผ่านเบราว์เซอร์แทนที่จะให้ลิงก์ของผลลัพธ์ โปรแกรมด้านล่าง −

from googlesearch import * import webbrowser #to search, will ask search query at the time of execution query = input("Input your query:") #iexplorer_path = r'C:\Program Files (x86)\Internet Explorer\iexplore.exe %s' chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s' for url in search(query, tld="co.in", num=1, stop = 1, pause = 2): webbrowser.open("https://google.com/search?q=%s" % query)

ผลลัพธ์

>>> =============== RESTART: C:/Python/Python361/google_search1.py =============== Input your query:Tutorialspoint

ข้างต้นผมค้นหาคิวรี "tutorialspoint" และมันจะโป๊ปอัพหน้าต่างเบราว์เซอร์พร้อมกับ −

การค้นหา Google ด้วยโค้ด Python