Python 2.7 BeautifulSoup4返回一个空集

时间:2016-12-21 04:17:29

标签: python-2.7 beautifulsoup python-requests bs4

我正在尝试使用bs4从谷歌搜索中获取链接,但我的代码返回一个空集。

import requests
from bs4 import BeautifulSoup

website = "https://www.google.co.uk/?gws_rd=ssl#q=science"

response=requests.get(website)

soup = BeautifulSoup(response.content)

link_info = soup.find_all("h3", {class": "r"})
print link_info

<h3 class="r">是所有结果的链接不仅仅是第一个结果的链接。

作为回复,我得到[],这适用于我尝试请求的任何其他类,包括<div class="rc">

以下是我所追求的内容,

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码

url = 'http://www.google.com/search?'
params = {'q': 'science'}
response = requests.get(url, params=params).content
soup = BeautifulSoup(response)
link_info = soup.find_all("h3", {"class": "r"})
print link_info