0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PANDASのURLをリンクチェックしHTTPステータスコードを返す

Posted at

はじめに

URLをPANDASのSeriesから、リンクチェックを行い、HTTPステータスコードを戻します。

変換元データ

import pandas as pd
df = pd.Series(['http://base1.nijl.ac.jp/~image/', 'http://archives.ih.otaru-uc.ac.jp/', 'https://repository.dl.itc.u-tokyo.ac.jp/search?search_type=2&q=6092'], index=['1', '2', '3'])

変換

import requests
from contextlib import suppress

def f_str(x):
    with suppress(Exception):
        r = requests.get(x)
        return str(r.status_code)

df['linkcheck'] = df.map(f_str)

結果

print(df)
1                              http://base1.nijl.ac.jp/~image/
2                           http://archives.ih.otaru-uc.ac.jp/
3            https://repository.dl.itc.u-tokyo.ac.jp/search...
linkcheck          1     403
2    None
3     200
dtype: object
dtype: object
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?