【问题标题】:Python Web Scraping - After Authentication - Traverse - Download from URL with no ExtensionPython Web Scraping - 身份验证后 - 遍历 - 从 URL 下载,没有扩展名
【发布时间】:2021-12-29 19:12:04
【问题描述】:

在 Chrome 中,我将登录到一个网站。
然后我将检查该站点,转到网络并清除现有的。 然后,我将单击一个链接并抓住存储 cookie 的标题。

在 Python 中,我将标题存储在字典中,并将其用于其余代码。

    def site_session(url, header_dict):
        session = requests.Session()
        t = session.get(url, headers=header_dict)
        return soup(t.text, 'html.parser')

    site = site_session('https://website.com', headers)
    # Scrape the Site as usual until I reach a file I can't download..

这是一个视频文件,但没有扩展名。
"https://website.sub.com/download"

单击此链接将打开保存对话框,我可以保存它。但不是在Python中.. 检查网络,它似乎重定向到我能够抓取的另一个网址。
"https://website.sub.com/download/file.mp4?jibberish-jibberish-jibberish

尝试将其缩短为“https://website.sub.com/download/file.mp4”无法打开。

with open(shortened_url, 'wb') as file:
    response = requests.get(shorened_url)
    file.write(response.content)

我已经尝试了完整的网址和缩短的网址并收到:
OSError: [Errno 22] 无效参数。

对此的任何帮助都会很棒!
谢谢!

【问题讨论】:

    标签: python-3.x web-scraping


    【解决方案1】:

    我必须在查询中使用完整的 url 并包含标题。

    # Read the Header of the first URL to get the file URL
    fake_url = requests.head(url, allow_redirects=True, headers=headers)
    
    # Get the real file URL
    real_url = requests.get(fake_url.url, headers=headers)
    
    # strip out the name from the url here since it's a loop
    
    # Open a file on pc and write the contents of the real_url
    with open(stripped_name, 'wb') as file:
        file.write(real_url.content)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2014-07-09
      • 2016-03-27
      • 1970-01-01
      相关资源
      最近更新 更多