簡體   English   中英

如何從 snscrape 獲取 substring(圖片 url)?

[英]How to get the substring (photo url) from the snscrape?

編輯,因為我意識到它也有 vedio url,我的問題是我怎樣才能在下面的循環中只得到照片 url? 我想添加一個名為 photourl 的屬性,它是來自媒體的完整 url。

import snscrape.modules.twitter as sntwitter
import pandas as pd

# Creating list to append tweet data to
attributes_container = []

# Using TwitterSearchScraper to scrape data and append tweets to list
for i,tweet in enumerate(sntwitter.TwitterSearchScraper('sex for grades since:2021-07-05 until:2022-07-06').get_items()):
    if i>150:
        break
    attributes_container.append([tweet.user.username, tweet.date, tweet.likeCount, tweet.sourceLabel, tweet.content, tweet.media])
    
# Creating a dataframe to load the list
tweets_df = pd.DataFrame(attributes_container, columns=["User", "Date Created", "Number of Likes", "Source of Tweet", "Tweet","media"])

當我使用 snscrape 從 twitter 抓取推文時,我想從照片中過濾照片圖像。 我得到如下媒體 object:

media=[Photo(previewUrl='https://pbs.twimg.com/media/FePrYL7WQAQDKEB?format=jpg, fullUrl='https://pbs.twimg.com/media/FePrYL7WQAQDKEB?format=jpg&name=large')]

那么我怎樣才能得到 PreviewUrl'https://pbs.twimg.com/media/FePrYL7WQAQDKEB?format=jpg, 和完整的 url sperately',

使用 python 代碼?

謝謝

您可以將for循環更改為:

for i,tweet in enumerate(sntwitter.TwitterSearchScraper('sex for grades since:2021-07-05 until:2022-07-06').get_items()):
    if i>150:
        break
    try:
      tweetMedia = tweet.media[0].fullUrl # .previewUrl if you want previewUrl
    except:
      tweetMedia = tweet.media # or None or '' or any default value 
    attributes_container.append([tweet.user.username, tweet.date, tweet.likeCount, tweet.sourceLabel, tweet.content, tweetMedia])

然后您將獲得每個推文行的網址 [如果有的話]。

如果你想把它全部放在append語句中,你可以將其更改為:

attributes_container.append([
    tweet.user.username, tweet.date, tweet.likeCount, 
    tweet.sourceLabel, tweet.content, 
        (tweet.media[0].fullUrl if tweet.media 
        and hasattr(tweet.media[0], 'fullUrl')
        else tweet.media)
])

[而不是添加try...except ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM