属性错误:“ str”对象没有属性“键”

时间:2019-11-17 13:21:08

标签: python beautifulsoup

我正在使用Python 3.7.4。学习剪贴。

我正在尝试抓取特定信息并自动在Google电子表格中进行更新。

有人可以告诉我如何纠正我的代码吗?

错误是:

soup=BeautifulSoup(urllib.request.urlopen(req).read())
Traceback (most recent call last):
  File "test.py", line 34, in <module>
    credentials = Credentials.from_service_account_info(credential_file_name, scopes=[scope])
  File "C:\Python37\lib\site-packages\google\oauth2\service_account.py", line 201, in from_service_account_info
    info, require=["client_email", "token_uri"]
  File "C:\Python37\lib\site-packages\google\auth\_service_account_info.py", line 46, in from_dict
    missing = keys_needed.difference(six.iterkeys(data))
  File "C:\Python37\lib\site-packages\six.py", line 583, in iterkeys
    return iter(d.keys(**kw))
AttributeError: 'str' object has no attribute 'keys'

代码如下:

from bs4 import BeautifulSoup
import urllib.request 
import gspread
from google.oauth2.service_account import Credentials

site='https://nathanbarry.com/authority/'
hdr={'User-Agent':'Mozilla/5.0'}
req=urllib.request.Request(site,headers=hdr)

soup=BeautifulSoup(urllib.request.urlopen(req).read())

def getfullprice(soup):
    divs=soup.find_all('div',id='complete-package')
    price=""
    for i in divs:
        price=i.a

    completeprice=(str(price).split('$',1)[1]).split('<',1)[0]
    return completeprice

def getvideoprice(soup):
    divs=soup.find_all('div',id='video-package')
    price=""
    for i in divs:
        price=i.a
    videoprice=(str(price).split('$',1)[1]).split('<',1)[0]
    return videoprice

fullprice = getfullprice(soup)
videoprice = getvideoprice(soup)

credential_file_name='credentials_xxxx-2db3e59ce9f3.json'
scope='https://docs.google.com/spreadsheets/d/1xxxxx'
credentials = Credentials.from_service_account_info(credential_file_name, scopes=[scope])

gc = gspread.authorize(credentials)
wks = gc.open("authority scraper").sheet1

wks.update_cell(1,2,"365")

0 个答案:

没有答案