I would like to try send requests.get to this website:
requests.get('https://rent.591.com.tw')
and I always get
I knew this is a common problem and tried different way but still failed. but all of other website is ok.
any suggestion?
I would like to try send requests.get to this website:
requests.get('https://rent.591.com.tw')
and I always get
I knew this is a common problem and tried different way but still failed. but all of other website is ok.
any suggestion?
Webservers are black boxes. They are permitted to return any valid HTTP response, based on your request, the time of day, the phase of the moon, or any other criteria they pick. If another HTTP client gets a different response, consistently, try to figure out what the differences are in the request that Python sends and the request the other client sends.
That means you need to:
I usually point my requests to a http://httpbin.org endpoint, have it record the request, and then experiment.
For requests
, there are several headers that are set automatically, and many of these you would not normally expect to have to change:
Host
; this must be set to the hostname you are contacting, so that it can properly multi-host different sitesContent-Length
and Content-Type
, for POST requests, are usually set from the arguments you pass to requests
. If these don't match, alter the arguments you pass in to requests
.Connection
: leave this to the client to manageCookies
: these are often set on an initial GET request, or after first logging into the site. Make sure you capture cookies with a requests.Session()
object and that you are logged in (supplied credentials the same way the browser did).Everything else is fair game. I usually start with the User-Agent header and work my way up from there.
In this case, the site is filtering on the user agent, it looks like they are blacklisting Python
, setting it to almost any other value already works:
>>> requests.get('https://rent.591.com.tw', headers={'User-Agent': 'Custom'})
Next, you need to take into account that requests
is not a browser. requests
is only a HTTP client, a browser does much, much more. A browser parses HTML for additional resources such as images, fonts, styling and scripts, loads those additional resources too, and executes scripts. Scripts can then alter what the browser displays and load additional resources. If your requests
results don't match what you see in the browser, but the initial request the browser makes matches, then you'll need to figure out what other resources the browser has loaded and make additional requests with requests
as needed. If all else fails, use a project like requests-html
, which lets you run a URL through an actual, headless Chromium browser.
The site you are trying to contact makes an additional AJAX request to https://rent.591.com.tw/home/search/rsList?is_new_list=1&type=1&kind=0&searchtype=1®ion=1
, take that into account if you are trying to scrape data from this site.
Last but not least, if a site is blocking scripts from making requests, they probably are either trying to enforce terms of service that prevent scraping, or because they have an API they rather have you use. Check for either, and take into consideration that you might be blocked more effectively if you continue to scrape the site anyway.