如何将Selenium与多个Url一起写入csv


import re
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import csv
oranev = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>1<[sS]*?[>](-|dd*.dd|dd*)"
oranX = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>X<[sS]*?[>](-|dd*.dd|dd*)"
orandep = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>2<[sS]*?[>](-|dd*.dd|dd*)"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://arsiv.mackolik.com/Mac/3495245/Boca-Juniors-Santos")
html_source = driver.page_source
driver.quit()
file = open('oranlar.csv', 'w+', newline='')
writer = csv.writer(file)
writer.writerow(['Ev', 'X', 'Dep'])

oranevoran = re.search(oranev, html_source, re.MULTILINE)
oranxoran = re.search(oranX, html_source, re.MULTILINE)
orandeporan = re.search(orandep, html_source, re.MULTILINE)
print(oranevoran.group(1))
print(oranxoran.group(1))
print(orandeporan.group(1))
writer.writerow([oranevoran.group(1), oranxoran.group(1), orandeporan.group(1)])

当我使用csv 中的顶部代码时

| Ev | X | Dep |
|----|---|-----|
|1.45 |3.10|4.60  |

我想自动化这个脚本。我想在完成第一个链接然后转到第二个链接时使用。这是我的另一个代码。但我不知道如何制作这个。

import re
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import csv
URLs =["http://arsiv.mackolik.com/Mac/3495245/Boca-Juniors-Santos","http://arsiv.mackolik.com/Mac/3482298/Aris-Saloniki-Volos-NFC"]

oranev = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>1<[sS]*?[>](-|dd*.dd|dd*)"
oranX = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>X<[sS]*?[>](-|dd*.dd|dd*)"
orandep = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>2<[sS]*?[>](-|dd*.dd|dd*)"
for index , url in enumerate(URLs):  #Stack here. i can't figure how to continue
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("url")
html_source = driver.page_source
driver.quit()
file = open('oranlar.csv', 'w+', newline='')
writer = csv.writer(file)
writer.writerow(['Ev', 'X', 'Dep'])

oranevoran = re.search(oranev, html_source, re.MULTILINE)
oranxoran = re.search(oranX, html_source, re.MULTILINE)
orandeporan = re.search(orandep, html_source, re.MULTILINE)
print(oranevoran.group(1))
print(oranxoran.group(1))
print(orandeporan.group(1))
writer.writerow([oranevoran.group(1), oranxoran.group(1), orandeporan.group(1)])

最后我想要我的csv文件像这个

| Ev | X | Dep |
|----|---|-----|
|1.45 |3.10|4.60  |
|next link number|next link number|next link number|

谢谢你的帮助。

这样的东西应该可以工作:

import re
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import csv
oranev = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>1<[sS]*?[>](-|dd*.dd|dd*)"
oranX = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>X<[sS]*?[>](-|dd*.dd|dd*)"
orandep = r"^(?!.*Handikaplı).*^(?!.*Yarı).*^(?!.*Alt).*Maç Sonucu.*$[sS]*?>2<[sS]*?[>](-|dd*.dd|dd*)"
driver = webdriver.Chrome(ChromeDriverManager().install())
URLs =['http://arsiv.mackolik.com/Mac/3495245/Boca-Juniors-Santos",http://arsiv.mackolik.com/Mac/3482298/Aris-Saloniki-Volos-NFC']
file = open('oranlar.csv', 'w+', newline='')
writer = csv.writer(file)
writer.writerow(['Ev', 'X', 'Dep'])
for link in URLs:
driver.get(link)
html_source = driver.page_source    
oranevoran = re.search(oranev, html_source, re.MULTILINE)
oranxoran = re.search(oranX, html_source, re.MULTILINE)
orandeporan = re.search(orandep, html_source, re.MULTILINE)
print(oranevoran.group(1))
print(oranxoran.group(1))
print(orandeporan.group(1))
writer.writerow([oranevoran.group(1), oranxoran.group(1), 
orandeporan.group(1)])
driver.quit()

最新更新