【问题标题】:Traceback Error while using selenium with python beautifulsoup library将 selenium 与 python beautifulsoup 库一起使用时出现回溯错误
【发布时间】:2017-04-13 10:05:22
【问题描述】:

我正在使用此代码从链接https://website.grader.com/results/www.dubizzle.com 中删除一些数据。因为带有标签的实际脚本我想在加载 15 秒后提取负载,所以有人建议我在代码中引入延迟。因此我使用此代码

代码如下

#!/usr/bin/python
import urllib
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
from dateutil.parser import parse
from datetime import timedelta
import MySQLdb
import re
import pdb
import sys
import string



driver = webdriver.Firefox()
driver.get('https://website.grader.com/results/dubizzle.com')
time.sleep(25)
html = driver.page_source
soup  = BeautifulSoup(html)


# print soup

Sizeofweb=""
try:

    Sizeofweb= soup.find('span', {'data-reactid': ".0.0.3.0.0.3.$0.1.1.0"}).text
    print Sizeofweb.get_text().encode("utf-8")

except StandardError as e:
    converted_date="Error was {0}".format(e)
    print converted_date

我提取的html部分如下

快照:https://www.dropbox.com/s/7dwbaiyizwa36m6/5.PNG?dl=0

<div class="result-value" data-reactid=".0.0.3.0.0.3.$0.1.1">
<span data-reactid=".0.0.3.0.0.3.$0.1.1.0">1.1</span>
<span class="result-value-unit" data-reactid=".0.0.3.0.0.3.$0.1.1.1">MB</span>
</div>

我安装了 geckodriver,方法是从此处下载并将其解压缩到 /home 目录,然后按照此处名为 @Ahn Smith 的人的建议为其提供路径 export PATH=$PATH:/home/geckodriver

现在当我运行程序时,它给出了这个错误

Traceback (most recent call last):
  File "ahmed.py", line 17, in <module>
    driver = webdriver.Firefox()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 140, in __init__
    self.service.start()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 20] Not a directory

【问题讨论】:

    标签: python selenium selenium-webdriver beautifulsoup bs4


    【解决方案1】:

    有两种方法可以将 Selenium 指向适当的 webdriver。您可以将其作为参数传递:

    driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
    

    或者你可以创建一个包含PATH的本地shell变量:

    $ export PATH=$PATH:/path/to/
    

    我认为您的问题是您将 PATH 变量导出到 geckodriver 而不是包含它的文件夹。

    【讨论】:

    • 路径/到/应该是原样吗? geckodriver 在主目录中,所以我应该把 driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
    • 如果作为executable_path 参数传递,您应该包含完整路径,包括geckodriver。如果将 PATH 变量添加到您的 shell,您应该只包含包含 geckodriver 的目录的路径。
    • 请检查我的答案以获取代码和错误。我已将 geckodriver 放在主目录中
    • 尝试重新启动。如果失败,请尝试检查您的权限以确保 geckodriver 是可执行的。 (或尝试将其复制到 /usr/local/sbin/)
    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2015-03-25
    相关资源
    最近更新 更多