driver.find_element_by_css_selector在页面结尾处运行缓慢

时间:2019-05-30 17:55:10

标签: python driver

我有一个from selenium import webdriver温度网络刮板,是使用Python在weather.com上工作的。页面开始处的刮板几乎立即找到正确的高温和低温。但是,到页面末尾,它变得越来越慢(到末尾需要7秒)。这可能是因为抓取工具必须通过更多的HTML才能找到正确的数据(?)。这是代码的主要部分:

high = driver.find_element_by_css_selector('#twc-scrollabe > table > tbody > tr:nth-child(' + str(j) + ') > td.temp > div > span:nth-child(1)').text
low = driver.find_element_by_css_selector('#twc-scrollabe > table > tbody > tr:nth-child(' + str(j) + ') > td.temp > div > span:nth-child(3)').text
    date = driver.find_element_by_css_selector('#twc-scrollabe > table > tbody > tr:nth-child(' + str(j) + ') > td:nth-child(2) > div > span').text

#auth > div > div > div > div > div > form > button

#twc-scrollabe > table > tbody > tr:nth-child(1) > td:nth-child(2) > div > span
#twc-scrollabe > table > tbody > tr:nth-child(2) > td:nth-child(2) > div > span

对此有简单(或复杂)的解决方法吗?而且,如果您认为没有简单的解决方案,那也会有很大的帮助(可能是这种情况?)

1 个答案:

答案 0 :(得分:1)

您要查找的内容是否由JavaScript生成?如果只有HTML,则可以避免使用无头浏览器,并使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <div id="num1"> </div> <div id="num2"> <button onclick="hallo();">I position red div at bottom: 150px</button> </div>requests

bs4

test.py

$ python test.py
Got response: 200
Today JUN 1  80°/61°
Sun   JUN 2  70°/47°
Mon   JUN 3  63°/45°
Tue   JUN 4  74°/57°
Wed   JUN 5  75°/64°
Thu   JUN 6  77°/63°
Fri   JUN 7  77°/64°
Sat   JUN 8  81°/66°
Sun   JUN 9  81°/65°
Mon   JUN 10 80°/63°
Tue   JUN 11 80°/63°
Wed   JUN 12 81°/62°
Thu   JUN 13 80°/63°
Fri   JUN 14 81°/63°
Sat   JUN 15 81°/63°
Total: 0.66s, request: 0.60s