Cant scrape google search results with beautifulsoup

北慕城南 提交于 2021-02-10 14:55:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!