【发布时间】:2021-04-26 12:09:23
【问题描述】:
一直在尝试从教程中获取 python 刮板,以在 twitter 上下载我喜欢的所有媒体(用于我忘记下载的参考图像和视频),但是在运行时它不会返回任何信息或媒体,这与我使用过的其他网站不同,我不知道是什么绊倒了它。有没有什么办法解决这一问题?这是我目前使用的代码:
import os
import requests as r
from bs4 import BeautifulSoup
# Request data from url
request = r.get('my twitter url')
soup = BeautifulSoup(request.text, "html.parser")
# source the images link which is to be downloaded
x = soup.select('img[src^="https://pbs.twimg.com/media/"]')
# generate links from the which the images are to be downloaded
links = []
for img in x:
links.append(img['src'])
# Create directory where the downloaded images are to be written
path = 'photos'
isDir = os.path.isdir(path)
if isDir:
print('Required directory is already available. Skipping folder creation..\n')
else:
print('Creating a directory\n')
os.mkdir('photos')
# Generate and save only up to 10 images to test code
i = 1
for index, img_link in enumerate(links):
if i <= 10:
print(f'Generating file {i}.jpg')
img_data = r.get(img_link).content
with open("photos/" + str(index + 1) + '_' + '.jpg', 'wb+') as f:
f.write(img_data)
i += 1
else:
break
【问题讨论】:
标签: python web-scraping twitter beautifulsoup