我遇到的问题是我正在运行的python脚本试图从打印的输出中获取PyTrends(https://github.com/GeneralMills/pytrends/)上的22个热门主题之一。我试图创建一个从1到22的随机数,然后使用它来选择在python shell中的第176-198行打印的22个结果之一。
import pytrends
import random
pytrend = TrendReq()
random = random.randint(1,22)
random = random + 99
itemList = list(pytrend.trending_searches())
Data = itemList.index(random) # This is one of the issue lines, as I cannot figure out how to index the output as needed.
Data = str(Data)
Data = Data[1:21] # An attempt at indexing output
print (Data)
这是我在Shell上的输出:
<bound method NDFrame.head of date exploreUrl \
0 20180504 /trends/explore?q=Free+Comic+Book+Day&date=now...
1 20180504 /trends/explore?q=Brad+Marchand&date=now+7-d&g...
2 20180504 /trends/explore?q=jrue+holiday&date=now+7-d&ge...
3 20180504 /trends/explore?q=Kentucky+Derby&date=now+7-d&...
4 20180504 /trends/explore?q=Cinco+de+Mayo&date=now+7-d&g...
5 20180504 /trends/explore?q=Warriors&date=now+7-d&geo=US
6 20180504 /trends/explore?q=Bruins&date=now+7-d&geo=US
7 20180504 /trends/explore?q=Rockets&date=now+7-d&geo=US
8 20180504 /trends/explore?q=Matt+Harvey&date=now+7-d&geo=US
9 20180504 /trends/explore?q=DJ+Khaled&date=now+7-d&geo=US
10 20180504 /trends/explore?q=Matthew+Lawrence&date=now+7-...
11 20180504 /trends/explore?q=junot+diaz&date=now+7-d&geo=US
12 20180504 /trends/explore?q=nashville+predators&date=now...
13 20180504 /trends/explore?q=albert+pujols&date=now+7-d&g...
14 20180504 /trends/explore?q=indians+vs+yankees&date=now+...
15 20180504 /trends/explore?q=zoe+saldana&date=now+7-d&geo=US
16 20180504 /trends/explore?q=Rihanna&date=now+7-d&geo=US
17 20180504 /trends/explore?q=Becky+Hammon&date=now+7-d&ge...
18 20180504 /trends/explore?q=dte+outage+map&date=now+7-d&...
19 20180504 /trends/explore?q=hawaii+news+now&date=now+7-d...
20 20180504 /trends/explore?q=Colton+Haynes&date=now+7-d&g...
21 20180504 /trends/explore?q=Audrey+Hepburn&date=now+7-d&...
22 20180504 /trends/explore?q=Carol+Burnett&date=now+7-d&g...
formattedTraffic hotnessColor hotnessLevel \
0 20,000+ #f0a049 2.0
1 20,000+ #f0a049 2.0
2 20,000+ #f0a049 2.0
3 2,000,000+ #d04108 5.0
4 1,000,000+ #db601e 4.0
5 500,000+ #db601e 4.0
6 200,000+ #e68033 3.0
7 200,000+ #e68033 3.0
8 200,000+ #e68033 3.0
9 200,000+ #e68033 3.0
10 100,000+ #e68033 3.0
11 100,000+ #e68033 3.0
12 100,000+ #e68033 3.0
13 100,000+ #e68033 3.0
14 100,000+ #e68033 3.0
15 50,000+ #f0a049 2.0
16 50,000+ #f0a049 2.0
17 50,000+ #f0a049 2.0
18 50,000+ #f0a049 2.0
19 50,000+ #f0a049 2.0
20 50,000+ #f0a049 2.0
21 50,000+ #f0a049 2.0
22 50,000+ #f0a049 2.0
imgLinkUrl imgSource \
0 https://wtop.com/entertainment/2018/05/grab-fr... WTOP
1 http://www.espn.com/nhl/story/_/id/23414142/nh... ESPN
2 https://www.slamonline.com/nba/jrue-holiday-an... SLAM Online
3 https://www.nbcnews.com/business/business-news... NBCNews.com
4 https://www.nytimes.com/2018/05/05/business/ci... New York Times
5 https://www.goldenstateofmind.com/2018/5/5/173... Golden State of Mind
6 https://www.bostonglobe.com/sports/bruins/2018... The Boston Globe
7 http://www.espn.com/nba/story/_/id/23409022/ho... ESPN
8 https://www.forbes.com/sites/tomvanriper/2018/... Forbes
9 http://people.com/music/dj-khaled-2015-video-w... PEOPLE.com
10 https://www.goodhousekeeping.com/life/a2015507... GoodHousekeeping.com
11 https://www.washingtonpost.com/news/arts-and-e... Washington Post
12 https://www.tennessean.com/story/sports/nhl/pr... The Tennessean
13 https://www.cbssports.com/mlb/news/leaderboard... CBSSports.com
14 https://www.mlb.com/news/miguel-andujar-yankee... MLB.com
15 http://people.com/movies/mila-kunis-gets-emoti... PEOPLE.com
16 http://www.bbc.com/news/newsbeat-44000486 BBC News
17 http://www.espn.com/nba/story/_/id/23407719/be... ESPN
18 https://www.lansingstatejournal.com/story/news... Lansing State Journal
19 http://www.hawaiinewsnow.com/story/38110613/li... Hawaii News Now
20 http://people.com/tv/colton-haynes-denies-rumo... PEOPLE.com
21 http://people.com/movies/see-audrey-hepburn-in... PEOPLE.com
22 https://www.vanityfair.com/hollywood/2018/05/c... Vanity Fair
imgUrl \
0 //t1.gstatic.com/images?q=tbn:ANd9GcRgX9VkY3X0...
1 //t2.gstatic.com/images?q=tbn:ANd9GcQNtvvQkzuu...
2 //t1.gstatic.com/images?q=tbn:ANd9GcSWuoUKvQM1...
3 //t0.gstatic.com/images?q=tbn:ANd9GcSvx53B96Jy...
4 //t3.gstatic.com/images?q=tbn:ANd9GcS7m8935VXh...
5 //t1.gstatic.com/images?q=tbn:ANd9GcQw4FYzAfaN...
6 //t1.gstatic.com/images?q=tbn:ANd9GcQKEOxhee7r...
7 //t1.gstatic.com/images?q=tbn:ANd9GcTMGOQfUc7u...
8 //t3.gstatic.com/images?q=tbn:ANd9GcQrbRgWqQM-...
9 //t3.gstatic.com/images?q=tbn:ANd9GcTH2gEcxXtQ...
10 //t2.gstatic.com/images?q=tbn:ANd9GcQuOq7biu30...
11 //t1.gstatic.com/images?q=tbn:ANd9GcQroHePQnEr...
12 //t0.gstatic.com/images?q=tbn:ANd9GcSgdsziSLo-...
13 //t1.gstatic.com/images?q=tbn:ANd9GcT8Z0CYLzOL...
14 //t0.gstatic.com/images?q=tbn:ANd9GcQJUrmvZbvz...
15 //t3.gstatic.com/images?q=tbn:ANd9GcSBQuX6A0c3...
16 //t0.gstatic.com/images?q=tbn:ANd9GcQU6AztveLs...
17 //t2.gstatic.com/images?q=tbn:ANd9GcQX6uw7bDSG...
18 //t2.gstatic.com/images?q=tbn:ANd9GcTKzcn18NOd...
19 //t1.gstatic.com/images?q=tbn:ANd9GcRSizKTqReb...
20 //t1.gstatic.com/images?q=tbn:ANd9GcTjJAoEQ0A2...
21 //t0.gstatic.com/images?q=tbn:ANd9GcRWzAeeA3c3...
22 //t0.gstatic.com/images?q=tbn:ANd9GcTCjUox_o9U...
newsArticlesList \
0 [{'title': 'Grab a freebie on <b>Free Comic Bo...
1 [{'title': 'NHL to give <b>Brad Marchand</b> e...
2 [{'title': 'Pelicans' <b>Jrue Holiday</b>:...
3 [{'title': '<b>Kentucky Derby</b> Field: No. 5...
4 [{'title': 'What Is <b>Cinco de Mayo</b>?', 'l...
5 [{'title': '<b>Warriors</b> deservedly get the...
6 [{'title': 'Dan Girardi lifts Lightning over <...
7 [{'title': '<b>Rockets</b> take 2-1 lead by bl...
8 [{'title': '<b>Matt Harvey</b> And Mets Just C...
9 [{'title': '<b>DJ Khaled</b> Faces Critics Aft...
10 [{'title': '<b>Matthew Lawrence</b> Proposed t...
11 [{'title': 'Pulitzer Prize-winning author <b>J...
12 [{'title': '<b>Predators</b> coach Peter Lavio...
13 [{'title': 'Leaderboarding: The astounding Hal...
14 [{'title': 'Andujar walks off Yanks to 13th wi...
15 [{'title': 'Mila Kunis Gets Emotional at BFF <...
16 [{'title': '<b>Rihanna</b> opens up about her ...
17 [{'title': 'Sources: Spurs assistant <b>Becky ...
18 [{'title': 'Crews restoring power quickly afte...
19 [{'title': 'LIST: Lava threat forces evacuatio...
20 [{'title': '<b>Colton Haynes</b> Shuts Down Ru...
21 [{'title': 'See <b>Audrey Hepburn</b> in Gorge...
22 [{'title': '<b>Carol Burnett</b> Wants to Be L...
relatedSearchesList safe \
0 [] 1.0
1 [] 1.0
2 [] 1.0
3 [{'query': 'Kentucky Derby 2018 horses', 'safe... 1.0
4 [{'query': 'Cinco De Mayo 2018 Events', 'safe'... 1.0
5 [{'query': 'Warriors Vs Pelicans', 'safe': Tru... 1.0
6 [{'query': 'Boston Bruins', 'safe': True}, {'q... 1.0
7 [{'query': 'Rockets Vs Jazz', 'safe': True}] 1.0
8 [] 1.0
9 [{'query': 'Dj Khaled Wife', 'safe': True}] 1.0
10 [{'query': 'Cheryl Burke', 'safe': True}] 1.0
11 [] 1.0
12 [{'query': 'Predators', 'safe': True}] 1.0
13 [] 1.0
14 [] 1.0
15 [] 1.0
16 [] 1.0
17 [] 1.0
18 [] 1.0
19 [] 1.0
20 [] 1.0
21 [] 1.0
22 [] 1.0
shareUrl startTime \
0 https://www.google.com/trends/hottrends?stt=Fr... 1.525540e+09
1 https://www.google.com/trends/hottrends?stt=Br... 1.525543e+09
2 https://www.google.com/trends/hottrends?stt=Jr... 1.525532e+09
3 https://www.google.com/trends/hottrends?stt=Ke... 1.525460e+09
4 https://www.google.com/trends/hottrends?stt=Ci... 1.525453e+09
5 https://www.google.com/trends/hottrends?stt=Wa... 1.525482e+09
6 https://www.google.com/trends/hottrends?stt=Br... 1.525486e+09
7 https://www.google.com/trends/hottrends?stt=Ro... 1.525493e+09
8 https://www.google.com/trends/hottrends?stt=Ma... 1.525468e+09
9 https://www.google.com/trends/hottrends?stt=DJ... 1.525475e+09
10 https://www.google.com/trends/hottrends?stt=Ma... 1.525439e+09
11 https://www.google.com/trends/hottrends?stt=Ju... 1.525457e+09
12 https://www.google.com/trends/hottrends?stt=Na... 1.525435e+09
13 https://www.google.com/trends/hottrends?stt=Al... 1.525439e+09
14 https://www.google.com/trends/hottrends?stt=In... 1.525486e+09
15 https://www.google.com/trends/hottrends?stt=Zo... 1.525446e+09
16 https://www.google.com/trends/hottrends?stt=Ri... 1.525432e+09
17 https://www.google.com/trends/hottrends?stt=Be... 1.525493e+09
18 https://www.google.com/trends/hottrends?stt=Dt... 1.525468e+09
19 https://www.google.com/trends/hottrends?stt=Ha... 1.525453e+09
20 https://www.google.com/trends/hottrends?stt=Co... 1.525489e+09
21 https://www.google.com/trends/hottrends?stt=Au... 1.525475e+09
22 https://www.google.com/trends/hottrends?stt=Ca... 1.525478e+09
title titleLinkUrl \
0 Free Comic Book Day //www.google.com/search?q=Free+Comic+Book+Day
1 Brad Marchand //www.google.com/search?q=Brad+Marchand
2 Jrue Holiday //www.google.com/search?q=Jrue+Holiday
3 Kentucky Derby //www.google.com/search?q=Kentucky+Derby
4 Cinco de Mayo //www.google.com/search?q=Cinco+de+Mayo
5 Warriors //www.google.com/search?q=Warriors
6 Bruins //www.google.com/search?q=Bruins
7 Rockets //www.google.com/search?q=Rockets
8 Matt Harvey //www.google.com/search?q=Matt+Harvey
9 DJ Khaled //www.google.com/search?q=DJ+Khaled
10 Matthew Lawrence //www.google.com/search?q=Matthew+Lawrence
11 Junot Diaz //www.google.com/search?q=Junot+Diaz
12 Nashville Predators //www.google.com/search?q=Nashville+Predators
13 Albert Pujols //www.google.com/search?q=Albert+Pujols
14 Indians Vs Yankees //www.google.com/search?q=Indians+Vs+Yankees
15 Zoe Saldana //www.google.com/search?q=Zoe+Saldana
16 Rihanna //www.google.com/search?q=Rihanna
17 Becky Hammon //www.google.com/search?q=Becky+Hammon
18 Dte Outage Map //www.google.com/search?q=Dte+Outage+Map
19 Hawaii News Now //www.google.com/search?q=Hawaii+News+Now
20 Colton Haynes //www.google.com/search?q=Colton+Haynes
21 Audrey Hepburn //www.google.com/search?q=Audrey+Hepburn
22 Carol Burnett //www.google.com/search?q=Carol+Burnett
trafficBucketLowerBound
0 20000.0
1 20000.0
2 20000.0
3 2000000.0
4 1000000.0
5 500000.0
6 200000.0
7 200000.0
8 200000.0
9 200000.0
10 100000.0
11 100000.0
12 100000.0
13 100000.0
14 100000.0
15 50000.0
16 50000.0
17 50000.0
18 50000.0
19 50000.0
20 50000.0
21 50000.0
22 50000.0 >
答案 0 :(得分:0)
pytrends将pandas数据帧作为输出返回。 Pandas数据帧有各种有用的子集化和索引方法,所以当你调用list和str而不是本机方法时,你会得到一些奇怪的结果。
要对数据帧进行随机抽样,您可以使用sample方法:
data.sample(n)
因此,要从请求中随机选择一行:
from pytrends.request import TrendReq
pytrend = TrendReq()
mydata = pytrend.trending_searches()
print(mydata.sample(1)) #or assign it, or get the required rows etc