当我试图通过BeautifulSoup获取时,<table>变为空

2024-06-08 04:03:52 发布

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

我正试图从网站https://www.kp.ru/best/kazan/abiturient_2018/ivmit/解析一个表。DevTools by Chrome向我展示了表是:

<div class="t431__table-wapper" data-auto-correct-mobile-width="false"> 
<table class="t431__table " style="">
...
</table>
</div>

但当我这么做的时候:

url = r"https://www.kp.ru/best/kazan/abiturient_2018/ivmit/"
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
tag = soup.find_all('div', {'class':r't431__table-wapper'})
print(tag)

它返回的结果就像<table>是空的:

[<div class="t431__table-wapper" data-auto-correct-mobile-width="false">
<table class="t431__table" style=""></table></div>, 
<div class="t431__table-wapper" data-auto-correct-mobile-width="false">
<table class="t431__table" style=""></table></div>,
<div class="t431__table-wapper" data-auto-correct-mobile-width="false">
<table class="t431__table" style=""></table></div>,
<div class="t431__table-wapper" data-auto-correct-mobile-width="false">
<table class="t431__table" style=""></table></div>]

是JavaScript还是什么?如何解决这个问题


Tags: httpsdivfalseautodatastylewwwtable
1条回答
网友
1楼 · 发布于 2024-06-08 04:03:52

你可以从另一个标签上得到这些信息

import requests
from bs4 import BeautifulSoup as bs

url = 'https://www.kp.ru/best/kazan/abiturient_2018/ivmit/'
soup = bs(requests.get(url).content, 'lxml')
print(soup.select_one('.t431__data-part2').text)

输出:

enter image description here

相关问题 更多 >

    热门问题