无法使用 BeautifulSoup 检索页面内容

Cannot Retrieve contents of a page using BeautifulSoup

我正在学习 BeautifulSoup 并尝试加载 this 网页的内容。我试图通过 inspect element 深入 HTML tags 来获取内容。

我使用了不同的代码片段来显示和检查我是否能够成功检索到内容。

以下代码片段产生了很好的结果:

from bs4 import BeautifulSoup
import requests

root = 'https://www.quora.com/topic/Graduate-Record-Examination-GRE-1'
r = requests.get(root)

soup = BeautifulSoup(r.text,'html.parser')

#**The following worked yielded some results :**

#1
a = soup.find_all('div',{'class':'feed'})
print(a)

#2
b = soup.find_all('div',{'class':'ContentWrapper'})
print(b)

#3
c = soup.find_all('div',{'class':'ContentWrapper'})
print(c)

#4
d = soup.find_all('div',{'class':'feed'})
print(d)

#5
e = soup.find_all('div',{'class':'TopicFeed'})
print(e)

但是,在深入了解之后,以下内容没有产生任何结果:

f = soup.find_all('div',{'class':'paged_list_wrapper'})
print(f)

它打印:[]

Content/HTML <div class='paged_list_wrapper'> 中的代码未打印。为什么?

站点可能被配置为根据 User-Agent 发送不同的页面。我 运行 遇到了和你一样的问题。它返回一个空列表。将通用用户代理添加到 headers 为我解决了这个问题。

from bs4 import BeautifulSoup
import requests
root = 'https://www.quora.com/topic/Graduate-Record-Examination-GRE-1'
headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.' }
r = requests.get(root,headers=headers)
soup = BeautifulSoup(r.text,'html.parser')
f = soup.findAll('div',{'class':'paged_list_wrapper'})
print(f)