2017-03-26 7 views
0

Webページが存在するかどうかを知る方法を探しています。 httlib2、urlparse、および使用要求のようなメソッドがたくさんあります。 Webページが 例えば https://www.thenews.com.pk/latest/category/sports/2015-09-21Pythonのスクラップ、Webページは存在しませんが、Webサイトは別のページにリダイレクトされます

存在doesnot場合は私の場合には、ウェブサイトは、ホームページに私をリダイレクトすることをキャッチする方法はありますか?

答えて

0

あなたが言いたいURLには、リダイレクトするリダイレクトコード(307)があります。ここをクリックしてください:

$ curl -i https://www.thenews.com.pk/latest/category/sports/2015-09-21 
HTTP/1.1 307 Temporary Redirect 
Date: Sun, 26 Mar 2017 10:13:39 GMT 
Content-Type: text/html; charset=UTF-8 
Transfer-Encoding: chunked 
Connection: keep-alive 
Set-Cookie: __cfduid=ddcd246615efb68a7c72c73f480ea81971490523219; expires=Mon, 26-Mar-18 10:13:39 GMT; path=/; domain=.thenews.com.pk; HttpOnly 
Set-Cookie: bf_session=b02fb5b6cc732dc6c3b60332288d0f1d4f9f7360; expires=Sun, 26-Mar-2017 11:13:39 GMT; Max-Age=3600; path=/; HttpOnly 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Location: https://www.thenews.com.pk/ 
X-Cacheable: YES 
X-Varnish: 654909723 
Age: 0 
Via: 1.1 varnish 
X-Age: 0 
X-Cache: MISS 
Access-Control-Allow-Origin: * 
Server: cloudflare-nginx 
CF-RAY: 345956a8be8a7289-AMS 
0

最後のurlがリダイレクトされたものか、リダイレクトがhistoryかどうかを確認できます。

>>> import requests 
>>> target_url = "https://www.thenews.com.pk/latest/category/sports/2015-09-21" 
>>> response = requests.get(target_url) 
>>> response.history[0].url 
u'https://www.thenews.com.pk/latest/category/sports/2015-09-21' 
>>> response.url 
u'https://www.thenews.com.pk/' 
>>> response.history and response.url == 'https://www.thenews.com.pk/' != target_url 
True 
+1

を使用してください。response.status_codeを使用してください。3で始まるコードはリダイレクトコードです。 –

+0

しかし、リダイレクトに従うと、最後に「200」が表示されます。 –

関連する問題