2017-04-16 34 views
0

我寫了代碼。我如何才能python twitter爬行(scarping)幾個關鍵字

但我認爲這不會起作用。

我想從「或」的概念中提取單詞,而不是「和」的概念。

似乎只提取'關鍵字1'。

如何更正?

import tweepy 
import time 
import os 

search_term = 'keyword1' 
search_term2= 'keyword2' 

lat = "37.6" 
lon = "127.0" 
radius = "200km" 
location = "%s,%s,%s" % (lat, lon, radius) 


API_key = "11111" 
API_secret = "22222" 
Access_token = "33333" 
Access_token_secret = "444" 


auth = tweepy.OAuthHandler(API_key, API_secret) 
auth.set_access_token(Access_token, Access_token_secret) 

api = tweepy.API(auth) 

c=tweepy.Cursor(api.search, 
      q=(search_term or search_term2), 
      rpp=1000, 
      geocode=location, 
      include_entities=True) 

data = {} 
i = 1 
for tweet in c.items(): 
    data['text'] = tweet.text 
    print(i, ":", data) 
    i += 1 
time.sleep(1) 


wfile = open(os.getcwd()+"/twtw2.txt", mode='w') 
data = {} 
i = 0  

for tweet in c.items(): 
    data['text'] = tweet.text 
    wfile.write(data['text']+'\n') 
    i += 1 
    time.sleep(1) 

wfile.close() 
+4

目前尚不清楚你到底在問什麼。此外,您不應將您的祕密和令牌包含在您公開發布的代碼中。 – jonrsharpe

回答

0

可能改變這一行

q=(search_term or search_term2), 

q="{}+OR+{}".format(search_term,search_term2), 
  • 案例事項這裏的OR操作
  • 輸入q作爲一個字符串,還不如說是一個表達式短路評估

順便說一下,您的憑據(來自您的帖子)也適用於我。

+0

謝謝!!!!!!!!!!!!!!!!!!!!!!!!!! – user7874671

相關問題