如何使用Selenium和BeautifulSoup在页面之间增加?

2024-10-01 09:39:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让我的代码在这个网站的页面中递增,但我似乎无法让它循环递增,而是做第一个页面,然后放弃。我做错什么了吗?你知道吗

         if(pageExist is not None):
              if(countitup != pageNum):
                 countitup = countitup + 1
                 driver.get('http://800notes.com/Phone.aspx/%s/%s' % (tele800,countitup))
                 delay = 4
                 scamNum = soup.find_all(text=re.compile(r"Scam"))
                 spamNum = soup.find_all(text=re.compile(r"Call type: Telemarketer"))
                 debtNum = soup.find_all(text=re.compile(r"Call type: Debt Collector"))
                 hospitalNum = soup.find_all(text=re.compile(r"Hospital"))
                 scamCount = len(scamNum) + scamCount
                 spamCount = len(spamNum) + spamCount
                 debtCount = len(debtNum) + debtCount
                 hospitalCount = len(hospitalNum) + hospitalCount
                 block = soup.find(text=re.compile(r"OctoNet HTTP filter"))
                 extrablock = soup.find(text=re.compile(r"returning an unknown error"))
                 type(block) is str 
                 type(extrablock) is str 
                 if(block is not None or extrablock is not None):
                    print("\n Damn. Gimme an hour to fix this.")
                    time.sleep(2000)

回购:https://github.com/GarnetSunset/Haircuttery/tree/Experimental


Tags: textrenonelenifistypenot
1条回答
网友
1楼 · 发布于 2024-10-01 09:39:56

pageExist is not None这似乎就是问题所在。 因为它检查页面是否为无,而且很可能永远不会为无。没有官方的方法来检查HTTP响应,但是我们可以使用类似的方法。你知道吗

if (soup.find_element_by_xpath('/html/body/p'[contains(text(),'400')])
#this will check if there's a 400 code in the p tag.

或者

if ('400' in soup.find_element_by_xpath('/html/body/p[1]').text)

我相信有其他方法可以做到这一点,但这是其中之一,所以这是这里唯一的问题。一旦修复了第一个if,就可以增加或保留代码的其余部分。你知道吗

我可能在我的代码中犯了一些错误(语法),因为我没有测试它,但逻辑适用),太棒了!

也代替了

             type(block) is str 
             type(extrablock) is str 

Python的方法是 isinstace

isinstance(block, str)
isinstance(extrablock, str)

至于time.sleep您可以使用WebDriverWait有两种可用的方法,隐式和显式等待,请看here。你知道吗

相关问题 更多 >