我使用 beautifulsoup4 编写了一个脚本,该脚本基本上从网页上的表格中获取密码列表。
问题是我的 python 脚本不会等待网页返回的内容,并且会中断或显示“列表索引超出范围”。代码如下:
ssl_lab_url = 'https://www.ssllabs.com/ssltest/analyze.html?d='+site
req = requests.get(ssl_lab_url)
data = req.text
soup = BeautifulSoup(data)
print CYELLOW+"Now Bringing in the LIST of cipher gathered from SSL LABS for "+str(ssl_lab_url)+CEND
for i in tqdm(range(10000)):
sleep(0.01)
table = soup.find_all('table',class_='reportTable', limit=5)[-1]
data = [ str(td.text.split()[0]) for td in table.select("td.tableLeft")]
print CGREEN+str(data)+CEND
time.sleep(1)
有时它不返回任何内容data
或者说:
Traceback (most recent call last):
File "multiple_scan_es.py", line 79, in <module>
scan_cipher_ssl(list_url )
File "multiple_scan_es.py", line 62, in scan_cipher_ssl
table = soup.find_all('table',class_='reportTable', limit=5)[-1]
IndexError: list index out of range
我需要在这里等待,该怎么做?
我以为这个页面使用 JavaScript 来获取数据,但它使用旧的 HTML 方法来刷新页面。
它添加了 HTML 标签<meta http-equiv="refresh" content='**time**; url>
浏览器将在之后重新加载页面time秒。
您必须检查此标签 - 如果找到它,那么您可以等待,并且必须再次加载页面。大多数情况下,您可以重新加载页面而无需等待并获取数据或再次找到此标签。
import requests
from bs4 import BeautifulSoup
import time
site = 'some_site_name.com'
url = 'https://www.ssllabs.com/ssltest/analyze.html?d='+site
# ---
while True:
r = requests.get(url)
soup = BeautifulSoup(r.text)
refresh = soup.find_all('meta', attrs={'http-equiv': 'refresh'})
#print 'refresh:', refresh
if not refresh:
break
#wait = int(refresh[0].get('content','0').split(';')[0])
#print 'wait:', wait
#time.sleep(wait)
# ---
table = soup.find_all('table', class_='reportTable', limit=5)
if table:
table = table[-1]
data = [str(td.text.split()[0]) for td in table.select("td.tableLeft")]
print str(data)
else:
print "[!] no data"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)