【问题标题】:How can i fix this error? (python 3.8) XxXx [duplicate]我该如何解决这个错误? (python 3.8) XxXx [重复]
【发布时间】:2020-06-03 18:09:11
【问题描述】:

我正在尝试在 Google 上抓取一些图片。我找到了这段代码,但它不起作用。 我该如何解决这个错误??

from urllib.request import urlopen
from urllib.request import urlretrieve
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
from selenium import webdriver

search= input('검색어:')
url = f'https://www.google.com/search?q={quote_plus(search)}&source=inms&tbm=isch&sa=X&ved=2haUKEwid64aF87LoAhUafd4KHcEtBZEQ_AUoAXoECBgQAw&biw=1536&bih=754'

driver = webdriver.Chrome()
driver.get(url)

html = driver.page_source
soup = BeautifulSoup(html)
img = soup.select('.rg_i.Q4LuWd.tx8vtf') 
n = 1
imgurl = []
for i in img:
    try:
        imgurl.append(i.attrs['src'])
    except KeyError:
        imgurl.append(i.attrs["data-src"])

for i in imgurl:
    urlretrieve(i,"크롤링 사진/"+ search + ".jpg")
    n +=1
    print(imgurl)

driver.close()

(검색어在韩语中的意思是“搜索词”。)

错误信息

Traceback (most recent call last):
  File "C:\Users\u\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Users\u\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\u\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 지정된 파일을 찾을 수 없습니다

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "yt.py", line 10, in <module>
    driver = webdriver.Chrome()
  File "C:\Users\u\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__    self.service.start()
  File "C:\Users\u\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

感谢您的帮助。


【问题讨论】:

标签: python python-3.x selenium selenium-webdriver


【解决方案1】:

安装 webdriver-manager

pip install webdriver-manager


from urllib.request import urlopen
from urllib.request import urlretrieve
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
from selenium import webdriver
# importing the installed package
from webdriver_manager.chrome import ChromeDriverManager

search= input('검색어:')
url = f'https://www.google.com/search?q={quote_plus(search)}&source=inms&tbm=isch&sa=X&ved=2haUKEwid64aF87LoAhUafd4KHcEtBZEQ_AUoAXoECBgQAw&biw=1536&bih=754'

                          # this will install chromedriver manager 
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)

html = driver.page_source
soup = BeautifulSoup(html)
img = soup.select('.rg_i.Q4LuWd.tx8vtf') 
n = 1
imgurl = []
for i in img:
    try:
        imgurl.append(i.attrs['src'])
    except KeyError:
        imgurl.append(i.attrs["data-src"])

for i in imgurl:
    urlretrieve(i,"크롤링 사진/"+ search + ".jpg")
    n +=1
    print(imgurl)

driver.close()

【讨论】:

    【解决方案2】:

    您似乎缺少chromedriver.exe,或者如果您已安装它,它不在您的路径中。 Chromedriver 是运行 Selenium 所必需的。

    按照此处的说明添加它...

    下载chromedriver:https://chromedriver.chromium.org/

    将其添加到路径:https://developers.refinitiv.com/sites/default/files/How%20To%20Add%20ChromeDriver%20To%20System%20Variables_0.pdf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2016-07-21
      • 2016-09-09
      • 1970-01-01
      相关资源
      最近更新 更多