我可以使用 GET 请求直接从页面抓取 Twitter 帖子图像吗?

问题描述 投票:0回答:1

我可以直接从 HTML 中抓取 Twitter 图像吗?

答案是否定的,或者至少根据我的尝试不是

import requests

# The URL for the GET request
url = '<twitter post link here'>
# Perform the GET request
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:

    # save the html content into a variable
    response = response.text
    print(response)

    with open("my_file.html", "w", encoding="utf-8") as f:
        # write the string into the file
        f.write(response)

else:
    print(f'Failed to get URL. Status code: {response.status_code}')

我编写了一个 Python 代码,尽管有请求,但实际上不需要任何依赖。 那么这段代码使用了Python的内置功能“open”,它允许创建和下载html文件。 该脚本将对 Twitter 帖子进行 get 调用

问题是它实际上所做的是返回一个空的 Twitter 页面,其中只有一个 Twitter 徽标可用。 发生这种情况可能是因为它是模态的

python http web-scraping twitter get
1个回答
0
投票

问题是 Twitter (x.com) 不会直接返回带有图像或原始帖子的网页。之后他们使用 JavaScript 加载实际的网页。 Twitter 试图阻止您通过简单的请求抓取页面,因为它们提供付费 API。 API 允许开发人员轻松访问内容。 即使您获得了实际的网页,它也不会包含任何图像。 html文档只指向图片的地址。

© www.soinside.com 2019 - 2024. All rights reserved.