如何过滤掉包含任何网址的推文?

时间:2018-04-26 13:47:45

标签: python twitter tweepy

我使用tweepy来获取与某个标签有关的推文,然后我将它们发送到某个黑盒子进行一些处理。但是,不应发送包含任何URL的推文。删除任何此类推文最合适的方式是什么?

3 个答案:

答案 0 :(得分:4)

在您的查询中添加private void Uploadfile(){ if (imageUri != null){ StorageReference fileReference = StorageRef.child(System.currentTimeMillis() + "." + getFileExtension(imageUri)); uploadtask = fileReference.putFile(imageUri) .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { Toast.makeText(gallery.this, "Upload successful", Toast.LENGTH_SHORT).show(); Upload upload= new Upload(Filename.getText().toString().trim(), taskSnapshot.getDownloadUrl().toString()); //create new database entry with unique image id String uploadId = DBREF.push().getKey(); DBREF.child(uploadId).setValue(upload); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Toast.makeText(gallery.this, e.getMessage(), Toast.LENGTH_SHORT).show(); } }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { @Override public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { } }); } else { Toast.makeText(this, "no file selected",Toast.LENGTH_SHORT).show(); } } 。 这将排除包含网址的推文。

答案 1 :(得分:1)

根据@Colin的建议,this question涵盖了使用正则表达式查找网址的问题。

示例代码段将是;

import re

// tweet_list is a list containing string you with to clean of urls
pattern = 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
filtered_tweet_list = [tweet for tweet in tweet_list if not re.findall(pattern, tweet)]

答案 2 :(得分:0)

您还可以在查询时排除带有网址的推文

if 'https:/' not in tweet.text:
    <do something eg. get tweet or in your case: send tweet>
相关问题