我目前正在浏览URL并在访问/抓取网站时抓取数据 .
有时一个网站将有一个不合理的长加载时间,没有错误,但不会完全加载,以允许chromedriver / urlopen完成/继续脚本,只是保持不稳定 .
动态测试元素的存在在这种情况下不起作用,因为页面不会完全加载,并且页面并不完全相同以测试固定元素(甚至不是像html或h1标签等丰富的标签) .
基本上我正在寻找一个代码,如果页面剂量加载,将在“x”秒后继续循环下一个循环
目前使用Selenium(chromedriver)和Beautifulsoup(BS4) .
def get_emails_from_list(links):
email=[]
for link in links:
driver.get(link)
html=driver.page_source
try:
raw = BeautifulSoup(html, 'html.parser').get_text()
emails = re.findall(r'[\w\.-]+@[\w\.-]+', raw)
for em in emails:
if em not in email:
email.append(emails)
except:
emails = re.findall(r'[\w\.-]+@[\w\.-]+', str(html))
for em in emails:
if em not in email:
email.append(emails)
try:
email2=list(itertools.chain(*email))
except:
email2=email
return email2
2 回答
执行此操作的最佳/常规方法是在套接字或用于网络io的库上设置超时 . 所以你应该考虑一下 .
如果不是,则可以使用线程或信号 . 这个使用信号 .
更新:
显然这不适用于Windows . 根据the documentation:在Windows上,
signal()
只能使用SIGABRT
,SIGFPE
,SIGILL
,SIGINT
,SIGSEGV
或SIGTERM
进行调用 .在循环内部,您可以等待几秒钟然后休息