'Python start for loop on next line if a statement is not true
with open('./links.txt', 'r') as f:
for line in f:
browser.get(line)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".jw-media")))
title = None
network = None
subnetwork = None
html = browser.page_source
if isinstance(title, str):
title = title.text
else:
with open('./notfound.txt', 'a') as h:
h.write(line)
h.write('\n')
h.close()
next(line)
For each line in f it is going to set the variables title, network and sub network to None and every time a page loads with the url (which is each line) from links.txt it will set the variables to the correct strings. The if statement will check whether the variables have changed and if they haven't I want it to go to the next string and start from the top settings the variables to None, loading the page, etc. is there any way of doing this?
Solution 1:[1]
The problem with your code is that the next(line) statement makes no sense, as line is not an iterator (the loop construct is already taking care of incrementing that for you). Also, using h.close() inside the with block is wrong, as close will be automatically called when you leave the with block.
Finally, it is not a good practice to open and close the file at every iteration of the loop.
The code below addresses these points:
with open('./links.txt', 'r') as f:
with open('./notfound.txt', 'a') as h:
for line in f:
browser.get(line.strip()) # Remove extraneous spaces, including the final '\n'
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".jw-media")))
title = browser.title # This is how you get the title of the page
network = None
subnetwork = None
html = browser.page_source
if isinstance(title, str): # Not sure what is your goal with this line
title = title.text
else:
h.write(line) # You don't need to add the '\n' because line already has it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nonDucor |
