2017-03-03 7 views
1

前に同様のquestionを投稿しました。私はPythonを使用してWebページを削り取ることができません

import requests 

url = 'https://www.zameen.com/' 
res = requests.get(url) 
data = res.text 
print(data) 

その応答は、私はBOTのどちらかだか、Javascriptが有効になっていないと言い、次のアプローチを使用してweb pageをこすりしようとしていました。だから、私はチェックがありますが、Javascriptが有効です。それは、再びロボットとして私を検出

b'<!DOCTYPE html>\n\n\t\n\n\t\n\t\n\t\n\n\t\n\t\n\n\t\n\t\n\t\n\n<head>\n<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">\n<meta http-equiv="cache-control" content="max-age=0" />\n<meta http-equiv="cache-control" content="no-cache" />\n<meta http-equiv="expires" content="0" />\n<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />\n<meta http-equiv="pragma" content="no-cache" />\n<meta http-equiv="refresh" content="10; url=/distil_r_captcha.html?Ref=/&amp;distil_RID=053235A2-0030-11E7-8429-B03805AB611E&amp;distil_TID=20170303163950" />\n<script type="text/javascript">\n\t(function(window){\n\t\ttry {\n\t\t\tif (typeof sessionStorage !== \'undefined\'){\n\t\t\t\tsessionStorage.setItem(\'distil_referrer\', document.referrer);\n\t\t\t}\n\t\t} catch (e){}\n\t})(window);\n</script>\n<script type="text/javascript" src="/ga368490.js" defer></script><style type="text/css">#d__fFH{position:absolute;top:-5000px;left:-5000px}#d__fF{font-family:serif;font-size:200px;visibility:hidden}#caexxxzxycbzutyvy{display:none!important}</style></head>\n<body>\n<div id="distil_ident_block">&nbsp;</div>\n</body>\n</html>\n' 

:だから私は、次のコード

from fake_useragent import UserAgent 
headers = {} 
headers['User-Agent'] = str(ua.chrome) 
web_page = requests.get(url,headers=headers) 
print(web_page.content) 

レスポンスと偽のユーザーエージェントを使用して別のアプローチを試してみました。そこで、私はウェブサイトからデータを取得できるかどうかを確認しました。

TRUE # Means I can fetch the data from the website. 

このWebページからデータを取得する方法があります:それから私はurllibは

from urllib import robotparser 

req = robotparser.RobotFileParser() 
req.set_url(url) 
req.read() 
print(req.can_fetch('*','https://www.zameen.com/')) 

戻り値からrobotparserを使用しましたか?ありがとう

+0

この回答を確認してください.com/questions/8049520/web-scraping-javascript-page-with-python – foobar

+0

私は何が起こっているのか分かりません。私はmechanizeを使って、robots_handleをfalseに設定しようとしましたが、なんらかの理由で405エラーが発生しました。リクエストと同じです。405エラーが発生しています – Shashank

+0

私はちょうど、質問のウェブサイトが、レスポンスで見つかったメタタグを介して、ボットによって訪問されないことを望みます: ' ' –

答えて

1

これにはBeautifulSoupとSeleniumドライバを使用できます。

from selenium import webdriver 
from bs4 import BeautifulSoup 

driver = webdriver.Firefox() # Could be any other browser you have the drivers for 
driver.get('https://zameen.com') 
html = driver.page_source 
code = BeautifulSoup(html, 'html5lib') 
print code 

だけでBS4とSeleniumをインストールすることを忘れないでください:私はあなたがこれを提供されたURLから、ページのソースを取得し、成功を得られます。http:// stackoverflowの

pip install bs4 

pip install selenium 
+0

Ps .:セレンの使用は、ページがあなたがボットであると思うのを妨げるでしょう。 – Dico

+0

残念ながら、 ''を返すメタデータと同じ応答を与えています: – muazfaiz

関連する問題