longxiaofei / spider-BaiduIndex

data sdk for baidu Index
MIT License
749 stars 229 forks source link

代码报错string indices must be integers #22

Closed yuyangyoung closed 4 years ago

yuyangyoung commented 4 years ago

test cookies返回正确,但是仍然报错

longxiaofei commented 4 years ago

test cookies返回正确,但是仍然报错

能把除cookie外的代码贴出来吗?

yuyangyoung commented 4 years ago

import os os.chdir(r"E:/百度指数爬虫/spider-BaiduIndex-master2/spider-BaiduIndex-master")

from baidu_index.utils import test_cookies from baidu_index import config from baidu_index import BaiduIndex, ExtendedBaiduIndex

cookies = """"""

if name == "main":

测试cookies是否配置正确

# True为配置成功,False为配置不成功
print(test_cookies(cookies))

Province_code = config.PROVINCE_CODE
City_code = config.CITY_CODE
keywords = ["暴雨"]
city_all_data=[]

for k,v in City_code.items():  
    baidu_index = BaiduIndex(
        keywords=keywords,
        start_date='2017-01-01',
        end_date='2017-12-31',
        cookies=cookies,
        area=int(v)
    )
    print(k)
    for index in baidu_index.get_index():
        baidu_data = index
        baidu_data["城市"] = k
        baidu_data["城市代码"] = v
        city_all_data.append(baidu_data)
    print(k)
longxiaofei commented 4 years ago

我本机跑这段代码没有任何问题。

不介意的话,可以将您现在使用的cookie发送到我的邮箱。

有可能是test_cookies这个方法不太准确,cookie可能并不是有效的cookie,当然也请确保test_cookies传入的cookie和BaiduIndex传入的cookie是同一个cookie。

之后会尝试添加一个自动登录获取cookie的方法。