Python AttributeError:' str'对象没有属性' get'

时间:2017-07-10 15:50:13

标签: python web-scraping attributes

我想抓取一个网站。

但是,发生了错误。

C:\Users\xxx\AppData\Local\Programs\Python\Python36\python.exe C:/Users/xxx/Desktop/scrap.py
Traceback (most recent call last):
  File "C:/Users/xxx/Desktop/scrap.py", line 10, in <module>
    driver.get('https://www.powderroom.co.kr/rankings/c1100')
AttributeError: 'str' object has no attribute 'get'

使用退出代码1完成处理

如何解决此错误?

感谢您的建议。

#py3.6,pycharm
import re
from bs4 import BeautifulSoup
from selenium import webdriver
import time

driver = webdriver.PhantomJS=("c:|phantomjs-2.1.1/windows/bin/phantomjs")

driver.get('https://www.powderroom.co.kr/rankings/c1100')
i = 0
while i < 8:
    i = i + 1
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(1)

bs = BeautifulSoup(driver.page_source, 'lxml')

titles = bs.findAll('div', attrs={'class':'fs-5 tc-gray-1'})
for title in titles:
    result = str(title.find_all(text=True))
    result = re.sub('[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\"]', '', result)
    print(result)
driver.quit()

2 个答案:

答案 0 :(得分:3)

看起来你有一个错字:

driver = webdriver.PhantomJS=("c:|phantomjs-2.1.1/windows/bin/phantomjs")
                            ^

我不认为等号应该在那里。

答案 1 :(得分:0)

用新鲜的蟒蛇环境进行了测试。 我得到了同样的错误,直到我安装了phantomjs。直到那一刻它看起来像python只是事情驱动程序是一个字符串变量,而不是具有get方法的特殊事物。

如果你不在这里使用anaconda就像幻影页面一样 http://phantomjs.org/download.html

如果你使用anaconda只是按照这里的说明 https://anaconda.org/trent/phantomjs

请告诉我这是否适合您。