Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

python - Why urlopen gives response 200 while request give 403?

to reproduce the situation

from urllib.request import Request, urlopen
site= 'https://www.ssense.com/en-hk/women/dresses'
hdr = {'User-Agent': 'Mozilla/5.0'}
req = Request(site,headers=hdr)
page = urlopen(req)
print(page.getcode())

the above return response 200, while request return 403 below

import requests
page = requests.get('https://www.ssense.com/en-hk/women/dresses',headers={'User-Agent': 'Mozilla/5.0'})
print(page.status_code)

Note: A funny issue is after request return 403, urlopen return 403 when executed immediately. Waiting for a few minutes, urlopen gives 200 again

I would like to ask

  1. is it safe to use urlopen in this case? as most people recommend request over urlopen
  2. if possible, please answer why this happen? as people mentioned they are similar

Thank you for your help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try with these headers:

accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9 accept-encoding:gzip, deflate, br accept-language:tr-TR,tr;q=0.9,en-US;q=0.8,en;q=0.7 cache-control:max-age=0 cookie:__cfduid=d56f8520d4458142c4dbd8eb36def3efe1609917283; _pxhd=416cf49200eb4c7587efba54abbaf59d0af556f200f05294430e0eea3876d8e2:d9d2e561-4fee-11eb-800c-4dfb4f618dd4; gdprCountry=true; cookieDisclaimerAccepted=false; visitorId=306deb7876a82deeb476f8a298596783b5be9297840f1af7e62890c32cb1e22d; forcedCountry=HK; preferredLanguage=en; is_markdown_ab_enabled=false; is_new_login_redirect_enabled=false; isp=tellcom iletisim hizmetleri a.s.; sid=5d2dd6802ab257fb54241b9abd68f965; lang=en_US; country=HK; _pxvid=d9d2e561-4fee-11eb-800c-4dfb4f618dd4; SSP_AB_fitpredictor=unallocated; _sp_ses.c6c8=*; shopping_bag=5ff56365a586bf5209e70946; _ga=GA1.2.1168695953.1609917285; _gid=GA1.2.1924545488.1609917285; rskxRunCookie=0; rCookie=15j0zp1rukg7162cv0n3bkjl36o18; __zlcmid=121jiLgFhxWISE7; _dd_s=rum=0&expire=1609918213509; _sp_id.c6c8=89852b8a-60e9-4408-904e-67e68c40c34e.1609917284.1.1609917314.1609917284.06c44494-676b-4024-91ca-03cb15d3fa22; lastRskxRun=1609917314864; _px2=eyJ1IjoiZWJiODBhZDAtNGZlZS0xMWViLTlmZGUtYWYzMTAyMzk3NDVlIiwidiI6ImQ5ZDJlNTYxLTRmZWUtMTFlYi04MDBjLTRkZmI0ZjYxOGRkNCIsInQiOjE2MDk5MTc2MTY1NzksImgiOiI5NDBhNDc5YzQzMWIxOTEwOGQ4ZDVkMDIyMDNhZjFlZjEyYzc5ZDg1MDI0ZmI3OWE5NWM3YjEyZjcyYjI1MjJiIn0= sec-ch-ua: "Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87" sec-ch-ua-mobile:?0 sec-fetch-dest:document sec-fetch-mode:navigate sec-fetch-site:none sec-fetch-user: ?1 upgrade-insecure-requests:1 user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...