Python Web Scraping Example: Extracting Google Search Results using requests_html
Here is an example of how to use the requests_html library in Python to scrape search results from Google:
from requests_html import HTMLSession
# Set up a session
session = HTMLSession()
# Define the search term and URL
search_term = 'python scrape example'
url = f'https://www.google.com/search?q={search_term}&num=10'
# Send a GET request to the URL
response = session.get(url)
# Render the JavaScript on the page
response.html.render()
# Find all search result titles and links
results = response.html.find('.g')
# Loop through the results and print the title and link
for result in results:
link = result.find('a', first=True).attrs['href']
title = result.find('h3', first=True).text
print(f'{title}: {link}')
In this example, we first create an HTMLSession object from the requests_html library. We then define the search term and the URL to send a GET request to. We then send the request and render the JavaScript on the page using the render() method. We then use the find() method to locate all search results on the page using the CSS selector '.g'. We then loop through the results and extract the link and title for each result using the find() method and print them to the console.
原文地址: https://www.cveoy.top/t/topic/lLfk 著作权归作者所有。请勿转载和采集!