使用 bs4 使用 Python 3.5 搜索 Google 时出错

Error in searching Google with Python 3.5 using bs4

我一直在学习 Beautiful Soup 和 Python 3.5,使用这个基本代码来搜索 Google.com

 from bs4 import BeautifulSoup as BS
 import urllib.request

 html = urllib.request.urlopen('http://www.google.com/search?q=python3')
 source = html.read()
 soup = BS( source , "lxml" )

 print(soup.prettify())

这给我一个错误:Image

但是通过将 URL 更改为 :

   html = urllib.request.urlopen('http://www.google.com/#q=python3')

显示正确的输出。

URL 如何改变代码的输出? 搜索 google.com 有什么特别的方法吗?

import requests
import json
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36'}
s = requests.get('http://www.google.com/search?q=python3',headers=headers)

soup =BeautifulSoup(s.content,'html.parser')
print(soup.prettify())

试试这个代码,我相信这就是你要找的