提问者:小点点

Web刮取隐藏元素


这是我通过检查得到的:

<div class="col-auto typing-module" id="test-container" style="display: block;">
<img src="assets/images/runner_girl2.png" alt="123">
<div class="test-notification test-notification-font-start" style="display: block;">Click or tap here and start typing!</div>
<div class="test-text-area test-text-area-font">

这是我的代码:

r = requests.get("https://www.typingtest.com/test.html?textfile=tiger.txt&minutes=1&mode=text&result_url=result.html")
soup = BeautifulSoup(r.text, 'html.parser')
lst_div = soup.find('div', {'class': 'col-auto typing-module'})
print(lst_div)

但输出是:

<div class="col-auto typing-module" id="test-container">
<img alt="123" src="assets/images/runner_girl2.png"/>
</div>


This line (<div class="test-text-area test-text-area-font">) is not in the lst

共1个答案

匿名用户

有时,当您从python请求一个web页面时,该页面与您从浏览器看到或检查的真实页面不同。 所以请求页面并保存它,然后检查它。

pg = requests.get("link")

f = open("sample.html","r")
f.write(pg.text)
f.close()

现在,在sample.html文件中检查并获取正确的id或类,然后编写代码。