【问题标题】:What is the correct way to call URL in python在python中调用URL的正确方法是什么
【发布时间】:2017-09-07 16:26:11
【问题描述】:

我有 csv 文件,并且将 csv 数据传递给 python 代码。在 csv 文件中有 URL 数据。在 python 中调用 URL 的正确方法是什么。收到错误Cannot navigate to invalid URL

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

代码:

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()

错误:

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)

【问题讨论】:

    标签: python python-3.x csv selenium web-scraping


    【解决方案1】:

    您的url 变量包含您要访问的链接。您的代码正在遍历一个字符串并对每个字符进行 driver.get() 调用。这基本上解释了错误。

    由于您已经在 for row in reader: 中循环访问数据,因此您不需要内部循环。只需使用driver.get(url)

    【讨论】:

    • 如果 mydata.csv 有两行,它会打开两个不同的浏览器。我想打开一个浏览器,这意味着每次 URL 都将替换为同一个浏览器。
    • @VenkateshPanabaka - 然后在循环之前实例化 Chrome 驱动程序driver = webdriver.Chrome(chrome_path)
    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多