এই আর্টিকেলে আমরা পাইথন কোড ব্যবহার করে গুগল সার্চ করার চেষ্টা করব, এটি হ্যান্ডি হয় যদি আপনি একটি পাইথন প্রজেক্টে কাজ করছেন এবং আপনাকে ওয়েব থেকে কিছু ডেটা অ্যাক্সেস করতে হয় এবং সার্চ রেজাল্ট (ওয়েব থেকে) আপনার প্রজেক্টের ভেতরে ব্যবহার করতে হয়।
পূর্বশর্ত:
- আপনার সিস্টেমে পাইথন ইনস্টল করা থাকতে হবে।
- 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
যদি উপরের সব পূর্বশর্ত পূরণ হয়, তবে আপনি গুগল সার্চ করার জন্য পাইথন কোড লিখতে পারেন।
নিচে প্রোগ্রামটি দেওয়া হলো যেখানে ইউজার একটি নির্দিষ্ট কীওয়ার্ড সার্চ করতে চায় (উদাহরণ: "AI in python" বা "Tutorialspoint") এবং গুগল সার্চ থেকে সব লিংক (ধরে নিই টপ ১০ রেজাল্ট) তার পাইথন প্রজেক্টে ব্যবহার করতে চায়।
# 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
সদৃশ ফলাফল আমরা ব্রাউজার থেকে সার্চ করলে পাই:

যদি আমরা কোয়েরি সার্চ রেজাল্ট সরাসরি ব্রাউজারের মাধ্যমে চাই লিংক দিয়ে না, তবে নিচের প্রোগ্রাম:
from googlesearch import *
import webbrowser
# সার্চ করতে হবে, এক্সিকিউশন সময় কোয়েরি জিজ্ঞেস করবে
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" কোয়েরি সার্চ করলাম, এবং এটি একটি ব্রাউজার উইন্ডো পপ-আপ করবে:
