我已经为网络疤痕编写了以下代码。没有if else循环部分的代码工作正常,我打算这样做。我有一个我想要抓取的网址列表,如果在任何网址中没有存在的元素,那么我必须绕过该网址继续下一步。我实现了绕过没有元素的url,但是我正常的抓取然后在else循环中不起作用。 任何帮助人?
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
urls= [
'http://www.marketsmojo.com/Stocks?StockId=1002687&Exchange=0'
]
f= open("lolly.txt","a+")
browser=webdriver.Chrome()
browser.maximize_window()
browser.get('http://www.marketsmojo.com/Stocks?StockId=565016&Exchange=0')
browser.find_element_by_xpath("//*[@id='step-0']/a/i").click()
for url in urls:
browser.get(url)
browser.execute_script("window.scrollTo(10,9500);")
browser.implicitly_wait(2000)
if browser.find_element_by_xpath("//div[contains(.,' No Shareholding data available ')]"):
continue
else:
add=browser.find_element_by_css_selector('#btnShareholdingDashboardFullDetails')
SearchButton = browser.find_element_by_css_selector('#btnShareholdingDashboardFullDetails')
Hover = ActionChains(browser).move_to_element(add).move_to_element(SearchButton)
Hover.click().perform()
browser.find_elements_by_css_selector('#allquarters > div > table')
add1 = browser.find_element_by_css_selector('#AllQuarters')
SearchButton1 = browser.find_element_by_css_selector('#AllQuarters')
Hover1 = ActionChains(browser).move_to_element(add).move_to_element(SearchButton1)
Hover1.click().perform()
data = []
for tr in browser.find_elements_by_css_selector('#allquarters > div > table'):
ths = tr.find_elements_by_tag_name('th')
tds = tr.find_elements_by_tag_name('td')
if ths:
data.append([th.text for th in ths])
if tds:
data.append([td.text for td in tds])
f.write(str(data))
browser.quit()
答案 0 :(得分:0)
对不起麻烦的人,只是导致打嗝的超时功能。 代码非常好。