无法使用BeautifulSoup检索页面内容

2024-04-20 06:47:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

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代码无法打印。为什么?你知道吗


Tags: 代码importdiv内容htmlfeedrootall
1条回答
网友
1楼 · 发布于 2024-04-20 06:47:08

站点可以配置为基于用户代理发送不同的页面。我遇到了和你一样的问题。它返回了一个空列表。在头文件中添加一个通用的用户代理为我解决了这个问题。你知道吗

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)

相关问题 更多 >