我抓取了推特数据,但没有使用 tweepy,我想获取每个用户在推文中使用的图像/视频的数量。我所拥有的:推文 URL:“https://twitter.com/user_screen_name/status/tweet_id,我还有 user_id 和推文(文本 + 链接 + 媒体)。
我想要做的是检查推文是否包含视频,如果是,则计算它并与图像相同。我注意到推文中使用的链接以“../t.co..”开头,因此它们基本上是重定向链接。此外,推文中显示的图像/视频基本上是重定向链接中包含的图像/视频(这就是我所理解的)
我尝试使用此代码进行图像计数,但没有得到任何结果:
import urllib
from bs4 import BeautifulSoup
from urllib.request import urlopen
def get_image_count(url):
soup = bs4.BeautifulSoup(urlopen((url))
images = soup.findAll('img')
file_types= '//img[contains(@src, ".jpg") or contains(@src, ".jpeg") or contains(@src, ".png")]'
# loop through all img elements found and store the urls with matching extensions
urls = list(x for x in images if x['src'].split('.')[-1] in file_types)
print(urls)
return len(urls)
当我使用此链接='https://twitter.com/fritzlabs/status/1369661296162054145'运行此代码时,这就是我得到的输出:
[<img alt="Twitter" height="38" src="https://abs.twimg.com/errors/logo46x38.png" srcset="https://abs.twimg.com/errors/logo46x38.png 1x, https://abs.twimg.com/errors/logo46x38@2x.png 2x" width="46"/>]
1
请问这里有什么帮助吗?我尝试了其他代码,但得到了相同的输出。谢谢你