[英]Web Scraping with python requests
I want to scrape https://sparrow.eoffice.gov.in/IPRSTATUS/IPRFiledSearch and download the entire set of PDF files that show up in the search results as on date (say 01-01-2016). 我想抓取https://sparrow.eoffice.gov.in/IPRSTATUS/IPRFiledSearch并下载截至日期显示在搜索结果中的整套PDF文件(例如2016年1月1日)。 The employee fields are optional.
员工字段是可选的。 On clicking search, the site throws up a list of all the employees.
单击搜索后,该网站将列出所有员工的列表。 I am unable to get the post method to work using python requests.
我无法使用python请求获取post方法。 Keep getting a 405 error.
持续收到405错误。 My code is below
我的代码如下
from bs4 import BeautifulSoup
import requests
url = "https://sparrow.eoffice.gov.in/IPRSTATUS/IPRFiledSearch"
data = {
'assessmentYearId':'vH4pgBbZ8y8rhOFBoM0g7w',
'empName':'',
'allotmentYear':'',
'cadreId':'',
'iprReportType':'cqZvyXc--mpmnRNfPp2k7w',
'userType':'JgPOADxEXU1jGi53Xa2vGQ',
'_csrf':'7819ec72-eedf-4290-ba70-6f2b14cc4b79'
}
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'en-US,en;q=0.8',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Content-Length':'184',
'Content-Type':'application/x-www-form-urlencoded',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
response = requests.post(url,data=data,headers=headers)
I'm not familiar with the website but I strongly suggest reading their policy before trying to scrape the content. 我对网站不熟悉,但强烈建议您在尝试抓取内容之前先阅读其政策。
In similar scenarios when you don't get the expected results by a simple post, using requests.Session
usually helps. 在类似的情况下,当您无法通过简单的帖子获得预期的结果时,请使用
requests.Session
通常会有所帮助。
The problem lay in my using the same csrf code. 问题出在我使用相同的csrf代码。 Needs to be changed with every request.
需要根据每个请求进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.