Skip to content
Advertisement

Unable to fetch tabular content from a site using requests

I’m trying to fetch tabular content from a webpage using the requests module. After navigating to that webpage, when I manually type 0466425389 right next to Company number and hit the search button, the table is produced accordingly. However, when I mimic the same using requests, I get the following response.

JavaScript

I’ve tried with:

JavaScript

How can I fetch tabular content from that site using requests?

Advertisement

Answer

From looking at the “action” attribute on the search form, the form appears to generate a new JSESSIONID every time it is opened, and this seems to be a required attribute. I had some success by including this in the URL.

You don’t need to explicitly set the headers other than the User-Agent.

I added some code: (a) to pull out the “action” attribute of the form using BeautifulSoup – you could do this with regex if you prefer, (b) to get the url from that redirection XML that you showed at the top of your question.

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement