简体   繁体   English

在python中调用URL的正确方法是什么

[英]What is the correct way to call URL in python

I have csv file and passing csv data has parameter to python code. 我有csv文件,并且将csv数据传递给python代码有参数。 In csv file has URL data. 在csv文件中有URL数据。 What is the correct way to call URL in python. 在python中调用URL的正确方法是什么。 Getting error Cannot navigate to invalid URL 出现错误Cannot navigate to invalid URL

CSV file CSV文件

ID,category,link
sports_shoes,sports-shoes,https://www.flipkart.com/mens-footwear/sports-shoes/pr?otracker=categorytree&page=1&sid=osp%2Ccil%2C1cu

Code: 码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time
import csv

with open('mydata.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        #print(row['ID'] ,row['category'],row['link'])
        url = row['link']
        print(url)
        chrome_path = r"C:\Users\Venkatesh\AppData\Local\Programs\Python\Python35\chromedriver.exe"
        driver = webdriver.Chrome(chrome_path)
        RegionIDArray = url
        data_list=[]
        data = []
        mobile_details_data = []
        delay = 30 # seconds
        for reg in RegionIDArray:
            driver.get(reg)
driver.quit()

Error: 错误:

Traceback (most recent call last):
  File ".\input_file.py", line 24, in <module>
    driver.get(reg)
  File "C:\Users\Venkatesh\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\remote\webdriver.
py", line 250, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\Venkatesh\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\remote\webdriver.
py", line 238, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Venkatesh\AppData\Local\Programs\Python\Python35\Lib\site-packages\selenium\webdriver\remote\errorhandl
er.py", line 193, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"messag
e":"Cannot navigate to invalid URL"}
  (Session info: chrome=57.0.2987.133)
  (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.2.9200 x86_64)

Your url variable contains the link which you want to access. 您的url变量包含您要访问的链接。 You code is looping through a string and making a driver.get() call on each character. 您的代码遍历字符串,并在每个字符上调用driver.get() Which basically explains the error. 基本上可以解释错误。

Since, you are already looping through your data in for row in reader: , you don't need the inner loop. 因为,您已经for row in reader:中的for row in reader:遍历数据,所以不需要内部循环。 Simply use driver.get(url) . 只需使用driver.get(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM