Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Thực hiện Google Search bằng code Python

Bài viết này hướng dẫn tìm kiếm Google bằng code Python, tiện lợi khi làm dự án Python cần truy cập dữ liệu từ web và sử dụng kết quả tìm kiếm trong dự án.

Điều kiện tiên quyết

  • Đã cài Python trên hệ thống.
  • Cài module google. Dùng pip:
    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
    

Sau khi hoàn tất, có thể viết code tìm kiếm Google.

Chương trình dưới đây: người dùng tìm từ khóa (ví dụ: "AI in python" hoặc "Tutorialspoint") và muốn tất cả link (giả sử top 10 kết quả) để dùng trong dự án 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()

Kết quả

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

Kết quả tương tự khi tìm kiếm qua trình duyệt.

Nếu muốn kết quả tìm kiếm trực tiếp qua trình duyệt thay vì trả về link:

from googlesearch import *
import webbrowser

query = input("Input your query:")
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)

Kết quả

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

Tìm kiếm "tutorialspoint", sẽ mở cửa sổ trình duyệt với kết quả.