Python 为什么BeautifulSoup不能在Google上获得所有html?

Python 为什么BeautifulSoup不能在Google上获得所有html?,python,beautifulsoup,Python,Beautifulsoup,我以前见过这个,但我从未见过任何与谷歌相关的东西。当在谷歌上搜索时,所有的链接和标题都放在h3标签中。然而,若我尝试使用BeautifulSoup,并没有一个h3标签出现,而且似乎有很多标签丢失了。我不认为这是一个JavaScript问题。我有什么遗漏吗 link = "http://google.com/search?q=" + input soup = BeautifulSoup(link, "lxml") for item in soup.find

我以前见过这个,但我从未见过任何与谷歌相关的东西。当在谷歌上搜索时,所有的链接和标题都放在h3标签中。然而,若我尝试使用BeautifulSoup,并没有一个h3标签出现,而且似乎有很多标签丢失了。我不认为这是一个JavaScript问题。我有什么遗漏吗

link = "http://google.com/search?q=" + input
soup = BeautifulSoup(link, "lxml")

for item in soup.find_all("h3"):
    print (item)

编辑:code

您需要首先使用
请求
模块获取网页的源代码,然后将其传递给
美化组
构造函数(也不要将
输入
用作变量名):

您也可以使输入失效安全。在google搜索URL中,您应该将空格替换为
+
,将
+
替换为
%20

import requests
from bs4 import BeautifulSoup

Input = input("Enter search string: ")
Input = Input.replace(" ","+").replace("+","%20")
link = "http://google.com/search?q=" + Input
html = requests.get(link).content
soup = BeautifulSoup(html, "lxml")

for item in soup.find_all("h3"):
    print (item.text)

如果您没有
请求

请使用
请求
获取数据。并缩小搜索范围,以便仅打印
标题

from bs4 import BeautifulSoup
import requests
link = "http://google.com/search?q=" + input("Enter search")
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0'}
r = requests.get(link, headers=headers)

soup = BeautifulSoup(r.text,'html5lib')

headings = soup.find_all('h3', class_ = 'LC20lb DKV0Md')

for heading in headings:
    print(heading.text)
输出:

Enter search>? beautifulsoup
Beautiful Soup Documentation — Beautiful Soup 4.9.0 ...
Beautiful Soup: We called him Tortoise because he taught us.
Beautiful Soup documentation - Crummy
beautifulsoup4 · PyPI
Intro to Beautiful Soup | Programming Historian
Beautiful Soup (HTML parser) - Wikipedia
Implementing Web Scraping in Python with BeautifulSoup ...
Tutorial: Web Scraping with Python Using Beautiful Soup
Beautiful Soup - Quick Guide - Tutorialspoint

你能展示一下你用来刮谷歌的代码吗?把它放进去。我想我早做了请核对答案me@DIDieureSeyee查看我的答案。如果您在
+
+
中添加代码来替换空格会更好,如果您在
%20
中输入带有空格的字符串,如
hello world
则可能会中断。输入:
输入搜索>?python的历史
结果:
python(编程语言)-Wikipedia Python历史-Wikipedia Python历史-Geeksforgeks Python历史-javatpoint Python教程:Python历史和哲学Python历史-教程点Python编程历史-教程点一般Python常见问题解答-Python 2.7.18文档
有趣。我编写了一些程序来使用Javascript和HTML搜索google,但它失败了,我希望python请求会失败,因为它工作得很顺利!我有一个关于一些事情的问题,但是,无论如何,我可以使更多的页面工作(超过5-7个结果)?还有,为什么是奇怪的班级?我认为所有的类在结果上都是不同的。我尝试了代码,结果什么也没有显示。这对所有的事情都有效吗?还是只是我这边的问题?
Enter search>? beautifulsoup
Beautiful Soup Documentation — Beautiful Soup 4.9.0 ...
Beautiful Soup: We called him Tortoise because he taught us.
Beautiful Soup documentation - Crummy
beautifulsoup4 · PyPI
Intro to Beautiful Soup | Programming Historian
Beautiful Soup (HTML parser) - Wikipedia
Implementing Web Scraping in Python with BeautifulSoup ...
Tutorial: Web Scraping with Python Using Beautiful Soup
Beautiful Soup - Quick Guide - Tutorialspoint