python - 如何使用 Beautiful Soup 从特定的 div 类中提取文本?
问题描述
所以基本上我想提取里面的文字
<div class = "examplediv">
example text
</div>
我用了
url='https://sample.com'
headers={"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
data = soup.find("div", {"class":"examplediv"})
clean_data = data.text
print(clean_data)
但它永远不会在终端中返回“无”。
我该怎么办?
解决方案
如果重复相同的类名,请尝试find_all
代替,这样您可能只会得到第一个项目。应该为您返回一个项目列表。否则你可能会处理在哪个下加载哪个不会通过模块呈现。您需要在这种情况下使用,或共享,以便我们可以手动检查其他方法。find
find_all
class
event
JavaScript
requests
selenium
url
推荐阅读
- sql - Postgres knex 查询连接列
- r - 在 ggplot 中使用 plotmath 表达式的两行注释
- php - Codeigniter where 和 where db 使用数组查询
- r - How to set the fontsize of a table footnote
- javascript - Angular, set dynamic style to button (hover)
- android - 如何访问 RecyclerView 位置
- rust - How to get the local time zone name in Rust?
- mule - creating an XML attribute from var in dataweave 2.0
- java - input arabic characters with scanner in maven project
- c++ - Why does this very simple and little C++ program terminates with -1073741819 (0xC0000005)?