Question: Python google search with urllib: I am working on a program where the user can input a search term and search google using urllib. The

Python google search with urllib:

I am working on a program where the user can input a search term and search google using urllib. The program seems to be stopping at this line but I am not sure why:

req = urllib.request.Request(url, None, headers)

with the error:

Traceback (most recent call last): File "C:\Users\Kristen Albrecht\OneDrive\Joint Documents\Education\UCLA Applications Programing\Python\Week 7\KristenAlbrechtProject7\google.py", line 19, in main() File "C:\Users\Kristen Albrecht\OneDrive\Joint Documents\Education\UCLA Applications Programing\Python\Week 7\KristenAlbrechtProject7\google.py", line 13, in main req = urllib.request.Request(url, None, headers) File "C:\Python\lib\urllib equest.py", line 329, in __init__ self.full_url = url File "C:\Python\lib\urllib equest.py", line 355, in full_url self._parse() File "C:\Python\lib\urllib equest.py", line 384, in _parse raise ValueError("unknown url type: %r" % self.full_url) ValueError: unknown url type: 'http//www.google.com/search?q=cat'

_____________________________________________________

goolge.py

import urllib.request

def main(): term = input('Enter a search term: ') url = 'http//www.google.com/search' value = {'q' : term} user_agent = 'Mozilla/5.0' headers = {'User-Agent' : user_agent} data = urllib.parse.urlencode(value) print(data) url = url + '?' + data print(url) req = urllib.request.Request(url, None, headers) response = urllib.request.urlopen(req) html = response.read() print(html) main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!