Pytrends 多个地理代码
Pytrends Multiple geo codes
我使用下面的代码导出美国多个州的搜索次数并收到错误“ResponseError:请求失败:Google 返回代码为 400 的响应。”
from pytrends.request import TrendReq
import pandas as pd
import time
startTime = time.time()
pytrend = TrendReq(hl='en-US', tz=360)
colnames = ["keywords"]
df = pd.read_csv("keyword_list.csv", names=colnames)
df2 = df["keywords"].values.tolist()
df2.remove("Keywords")
dataset = []
for x in range(0,len(df2)):
keywords = [df2[x]]
pytrend.build_payload(
kw_list=keywords,
cat=0,
timeframe='2020-01-01 2020-02-01',
geo='US-MA,US-TX,US-NY,US-WA')
data = pytrend.interest_over_time()
if not data.empty:
data = data.drop(labels=['isPartial'],axis='columns')
dataset.append(data)
result = pd.concat(dataset, axis=1)
result.to_csv("US.csv")
问题出在geo参数上,一次只能提供一个,不是全部,你可以这样试试
from pytrends.request import TrendReq
import pandas as pd
import time
startTime = time.time()
pytrend = TrendReq(hl='en-US', tz=360)
colnames = ["keywords"]
df = pd.read_csv("keyword_list.csv", names=colnames)
df2 = df["keywords"].values.tolist()
df2.remove("Keywords")
dataset = []
for geo_code in ['US-MA','US-TX','US-NY','US-WA']:
for x in range(0,len(df2)):
keywords = [df2[x]]
pytrend.build_payload(
kw_list=keywords,
cat=0,
timeframe='2020-01-01 2020-02-01',
geo=geo_code)
data = pytrend.interest_over_time()
if not data.empty:
data = data.drop(labels=['isPartial'],axis='columns')
data['geo']=geo_code
dataset.append(data)
result = pd.concat(dataset)
result.to_csv("US.csv")
我使用下面的代码导出美国多个州的搜索次数并收到错误“ResponseError:请求失败:Google 返回代码为 400 的响应。”
from pytrends.request import TrendReq
import pandas as pd
import time
startTime = time.time()
pytrend = TrendReq(hl='en-US', tz=360)
colnames = ["keywords"]
df = pd.read_csv("keyword_list.csv", names=colnames)
df2 = df["keywords"].values.tolist()
df2.remove("Keywords")
dataset = []
for x in range(0,len(df2)):
keywords = [df2[x]]
pytrend.build_payload(
kw_list=keywords,
cat=0,
timeframe='2020-01-01 2020-02-01',
geo='US-MA,US-TX,US-NY,US-WA')
data = pytrend.interest_over_time()
if not data.empty:
data = data.drop(labels=['isPartial'],axis='columns')
dataset.append(data)
result = pd.concat(dataset, axis=1)
result.to_csv("US.csv")
问题出在geo参数上,一次只能提供一个,不是全部,你可以这样试试
from pytrends.request import TrendReq
import pandas as pd
import time
startTime = time.time()
pytrend = TrendReq(hl='en-US', tz=360)
colnames = ["keywords"]
df = pd.read_csv("keyword_list.csv", names=colnames)
df2 = df["keywords"].values.tolist()
df2.remove("Keywords")
dataset = []
for geo_code in ['US-MA','US-TX','US-NY','US-WA']:
for x in range(0,len(df2)):
keywords = [df2[x]]
pytrend.build_payload(
kw_list=keywords,
cat=0,
timeframe='2020-01-01 2020-02-01',
geo=geo_code)
data = pytrend.interest_over_time()
if not data.empty:
data = data.drop(labels=['isPartial'],axis='columns')
data['geo']=geo_code
dataset.append(data)
result = pd.concat(dataset)
result.to_csv("US.csv")