问题
I want to scrape google search results , but whenever i try to do so, the program returns an empty list
from bs4 import BeautifulSoup
import requests
keyWord = input("Input Your KeyWord :")
url = f'https://www.google.com/search?q={keyWord}'
src = requests.get(url).text
soup = BeautifulSoup(src, 'lxml')
container = soup.findAll('div', class_='g')
print(container)
回答1:
To get correct result page from google, specify User-Agent
http header. For only english results put hl=en
parameter in URL:
from bs4 import BeautifulSoup
import requests
keyWord = input("Input Your KeyWord :")
url = f'https://www.google.com/search?hl=en&q={keyWord}'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0'}
src = requests.get(url, headers=headers).text
soup = BeautifulSoup(src, 'lxml')
containers = soup.findAll('div', class_='g')
for c in containers:
print(c.get_text(strip=True, separator=' '))
来源:https://stackoverflow.com/questions/62466340/cant-scrape-google-search-results-with-beautifulsoup