繁体   English   中英

InvalidArgumentException:使用从 CSV 读取的 Selenium 和 Pandas 抓取 url 的无效参数错误

[英]InvalidArgumentException: invalid argument error using Selenium and Pandas scraping urls reading from a CSV

我试图抓取一个网站,其中可能的网址在 csv 中。 因此,在通过 for 循环调用我的方法之后,我将打开 url 并废弃该站点的内容。

但由于某种原因,我无法循环打开网址

这是我的代码:

from selenium import webdriver
import time
import pandas as pd

chrome_options = webdriver.ChromeOptions();
chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')

driver = webdriver.Chrome(chrome_options=chrome_options)

csv_data = pd.read_csv("./data_new.csv")
df = pd.DataFrame(csv_data);

urls = df['url'] 

print(urls[: 5])

def scrap_site(url):
    
    print("Recived URL ---> ", url)
    
    driver.get(url)
    time.sleep(5)

    driver.quit()

for url in urls:
    print("URL ---> ", url)
    scrap_site(url)
    

我得到的控制台错误

Traceback (most recent call last):

  File "/media/sf_shared_folder/scrape_project/course.py", line 56, in <module>
    scrap_site(url)

  File "/media/sf_shared_folder/scrape_project/course.py", line 35, in scrap_site
    driver.get(url)

  File "/home/mujib/anaconda3/envs/spyder/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})

  File "/home/mujib/anaconda3/envs/spyder/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)

  File "/home/mujib/anaconda3/envs/spyder/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)

InvalidArgumentException: invalid argument
  (Session info: chrome=96.0.4664.45)

CSV 文件具有以下格式

url
http://www.somesite.com
http://www.someothersite.com

对于 CSV 文件,例如:

CSV

通读pandas并将其存储在DataFrame中并使用以下命令创建列表

urls = df['url']

在打印列表时,您将观察到列表项包含列索引:

urls = df['urls']
print(urls)

控制台 Output:

0    https://stackoverflow.com/
1    https://www.google.com/
Name: urls, dtype: object

此处列表中的 url 不是有效的 url。 因此,您会看到命令的错误:

self.execute(Command.GET, {'url': url})

解决方案

实际上,您需要使用tolist()抑制列索引,如下所示:

urls = df['urls'].tolist()
print(urls)

控制台 Output:

['https://stackoverflow.com/', 'https://www.google.com/']

此 url 列表是通过get()调用以进行进一步抓取的有效 url。

您需要将driver = webdriver.Chrome(chrome_options=chrome_options)放入循环中。 一旦driver.quit()被调用,你必须再次定义驱动程序。

from selenium import webdriver
import time
import pandas as pd

chrome_options = webdriver.ChromeOptions();
chrome_options.add_argument('--user-agent="Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166"')

# driver = webdriver.Chrome(chrome_options=chrome_options)

csv_data = pd.read_csv("./data_new.csv")
df = pd.DataFrame(csv_data);

# urls = df['url'] 
urls = ['https://stackoverflow.com/',
    'https://www.yahoo.com/']

print(urls[: 5])

def scrap_site(url):
    ############## OPEN THE DRIVER HERE ##############
    driver = webdriver.Chrome(chrome_options=chrome_options)
    ############## OPEN THE DRIVER HERE ##############
    
    print("Recived URL ---> ", url)
    
    driver.get(url)
    time.sleep(5)

    driver.quit()

for url in urls:
    print("URL ---> ", url)
    scrap_site(url)

暂无
暂无

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

相关问题 selenium.common.exceptions.InvalidArgumentException:消息:无效参数错误调用 get() 与使用 Selenium Python 从文本文件读取的 url 读取来自.csv 的 URL 列表,用于使用 Python、BeautifulSoup、Z251D2BBFE9A3B95EAZCE5696 进行抓取 selenium.common.exceptions.InvalidArgumentException:消息:使用 Selenium Webdriver 通过 Python 调用 get() 时参数无效错误 selenium.common.exceptions.InvalidArgumentException:消息:参数无效:使用 find_element('username') 时出现无效定位器错误 Selenium Python selenium.common.exceptions.InvalidArgumentException:消息:在遍历 url 列表并作为参数传递给 get() 时参数无效 selenium.common.exceptions.InvalidArgumentException:消息:无效参数:使用 Selenium 调用 send_keys() 时未找到文件错误 selenium.common.exceptions.InvalidArgumentException:消息:无效参数:使用 Selenium 上传文件时找不到文件错误 Python Selenium InvalidArgumentException:无效参数:“名称”必须是字符串 InvalidArgumentException:消息:无效参数:用户数据目录已在使用错误使用--user-data-dir使用Selenium启动Chrome selenium.common.exceptions.InvalidArgumentException:消息:无效参数:在 Selenium 中切换帧时缺少“元素”错误
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM