You can always climb several elements up or down to test out using next_sibling
/previous_sibling
or next_element
/previous_element
. All results are in the <div>
element with .tF2Cxc
class.
Scrape URLs is as easy as:
- make a
for loop
in combo with bs4
.select()
method that which takes СSS selectors as an input.
- call
.yuRUbf
CSS selector with .select_one()
method.
- call
<a>
tag with href
attribute.
for result in soup.select('.tF2Cxc'):
link = result.select_one('.yuRUbf').a['href']
Code and example in the online IDE:
import requests, lxml
from bs4 import BeautifulSoup
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3538.102 Safari/537.36 Edge/18.19582"
}
params = {'q': 'cyber security'}
html = requests.get('https://www.google.com/search', headers=headers, params=params).text
soup = BeautifulSoup(html, 'lxml')
# containver with all needed data
for result in soup.select('.tF2Cxc'):
link = result.select_one('.yuRUbf').a['href'] # or ('.yuRUbf a')['href']
print(link)
# output:
'''
https://www.kaspersky.com/resource-center/definitions/what-is-cyber-security
https://www.cisco.com/c/en/us/products/security/what-is-cybersecurity.html
https://digitalguardian.com/blog/what-cyber-security
https://searchsecurity.techtarget.com/definition/cybersecurity
https://www.cisa.gov/cybersecurity
https://en.wikipedia.org/wiki/Computer_security
https://www.csoonline.com/article/3482001/what-is-cyber-security-types-careers-salary-and-certification.html
https://staysafeonline.org/
'''
Alternatively, you can do the same thing using Google Organic Results API from SerpApi. It's a paid API with a free plan.
Code to integrate:
params = {
"api_key": os.getenv("API_KEY"), # environment for API_KEY
"engine": "google", # search engine
"q": "cyber security", # query
"hl": "en", # defining a language
}
search = GoogleSearch(params)
results = search.get_dict()
for result in results['organic_results']:
link = result['link']
print(link)
# output:
'''
https://www.kaspersky.com/resource-center/definitions/what-is-cyber-security
https://digitalguardian.com/blog/what-cyber-security
https://en.wikipedia.org/wiki/Computer_security
https://www.cisco.com/c/en/us/products/security/what-is-cybersecurity.html
https://staysafeonline.org/
https://searchsecurity.techtarget.com/definition/cybersecurity
https://www.cisa.gov/cybersecurity
https://www.csoonline.com/article/3482001/what-is-cyber-security-types-careers-salary-and-certification.html
'''
Disclaimer, I work for SerpApi.