【问题标题】:<table> becomes empty, when I'm trying to get it via BeautifulSoup当我尝试通过 BeautifulSoup 获取 <table> 时,它变为空
【发布时间】:2019-08-14 12:19:58
【问题描述】:

我正在尝试解析来自网站https://www.kp.ru/best/kazan/abiturient_2018/ivmit/ 的表格。 Chrome 的 DevTools 向我显示该表是:

<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)

它返回我就像&lt;table&gt; 是空的:

[<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 还是什么?如何解决这个问题?

【问题讨论】:

  • 访问网站。如果它通过 javascript 加载数据,您需要等待整个页面加载完毕,然后才能找到您要查找的表数据。美丽的汤不能这样做。尝试一些成熟的浏览器模拟器 - f.e. Selenium 并等到整个页面加载完毕后再进行抓取。

标签: python parsing web-scraping beautifulsoup screen-scraping


【解决方案1】:

您可以从另一个标签获取该信息

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)

输出:

【讨论】:

    猜你喜欢
    • 2014-09-24
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    相关资源
    最近更新 更多