[英]selenium python how to read url from csv and convert list from pure string?
我試過這段代碼:
with open('products_url.csv', 'r') as read_obj:
csv_reader = reader(read_obj)
header = next(csv_reader)
# Check file as empty
if header != None:
# Iterate over each row after the header in the csv
for row in csv_reader:
# row variable is a list that represents a row in csv
print(row)
結果:
['https://www.zzounds.com/item--RNEONE']
['https://www.zzounds.com/item--CVTGIGBARMOVE']
['https://www.zzounds.com/item--AKAMPCLIVE2XL']
['https://www.zzounds.com/item--RNETWELVEMK2']
['https://www.zzounds.com/item--AKAMPCONE']
['https://www.zzounds.com/item--NUMMIXSTREAMPRO']
但我的預期結果將是純字符串而不是列表。
https://www.zzounds.com/item--RNEONE
https://www.zzounds.com/item--CVTGIGBARMOVE
https://www.zzounds.com/item--AKAMPCLIVE2XL
https://www.zzounds.com/item--RNETWELVEMK2
https://www.zzounds.com/item--AKAMPCONE
https://www.zzounds.com/item--NUMMIXSTREAMPRO
如何獲得上述結果,以便我可以在 selenium python 中使用driver.get(url)
?
我的 csv 看起來像這樣:
url
https://www.zzounds.com/item--RNEONE
https://www.zzounds.com/item--CVTGIGBARMOVE
https://www.zzounds.com/item--AKAMPCLIVE2XL
https://www.zzounds.com/item--RNETWELVEMK2
https://www.zzounds.com/item--AKAMPCONE
https://www.zzounds.com/item--NUMMIXSTREAMPRO
問題措辭不佳。 考慮編輯?
問題是您將結果打印到控制台,而不是將其保存在列表中。 您可以通過執行以下操作將其保存在列表中:
with open('products_url.csv', 'r') as read_obj:
csv_reader = reader(read_obj)
header = next(csv_reader)
# Check file as empty
if header is not None:
def _gen():
# Iterate over each row after the header in the csv
for row in csv_reader:
# row variable is a list that represents a row in csv
url, = row
yield url
urls = list(_gen())
print(urls)
或者,如果您知道語法,則可以使用列表推導
with open('products_url.csv', 'r') as read_obj:
csv_reader = reader(read_obj)
header = next(csv_reader)
# Check file as empty
if header is not None:
# Iterate over each row after the header in the csv
urls = [url for url, in csv_reader]
print(urls)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.