无法使用 BeautifulSoup检索页面内容



我正在学习BeautifulSoup并尝试加载此网页的内容。我试图通过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)

它打印:[]

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

站点可以配置为根据用户代理发送不同的页面。我遇到了和你一样的问题。它返回了一个空列表。向标题添加通用用户代理为我解决了这个问题。

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)

最新更新