Skip to content
Advertisement

Exact website links from google through BeautifulSoup

I want to search google using BeautifulSoup and open the first link. But when I opened the link it shows error. The reason i think is that because google is not providing exact link of website, it has added several parameters in url. How to get exact url?

When i tried to use cite tag it worked but for big urls its creating problem.

The first link which i get using soup.h3.a[‘href’][7:] is: ‘http://www.wikipedia.com/wiki/White_holes&sa=U&ved=0ahUKEwi_oYLLm_rUAhWJNI8KHa5SClsQFggbMAI&usg=AFQjCNGN-vlBvbJ9OPrnq40d0_b8M0KFJQ

Here is my code:

import requests
from bs4 import Beautifulsoup
r = requests.get('https://www.google.com/search?q=site:wikipedia.com+Black+hole&gbv=1&sei=YwHNVpHLOYiWmQHk3K24Cw')
soup = BeautifulSoup(r.text, "html.parser")
print(soup.h3.a['href'][7:])

Advertisement

Answer

You could split the returned string:

url = soup.h3.a['href'][7:].split('&')
print(url[0])
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement