[英]Web Scraping with python requests
我想抓取https://sparrow.eoffice.gov.in/IPRSTATUS/IPRFiledSearch並下載截至日期顯示在搜索結果中的整套PDF文件(例如2016年1月1日)。 員工字段是可選的。 單擊搜索后,該網站將列出所有員工的列表。 我無法使用python請求獲取post方法。 持續收到405錯誤。 我的代碼如下
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)
我對網站不熟悉,但強烈建議您在嘗試抓取內容之前先閱讀其政策。
在類似的情況下,當您無法通過簡單的帖子獲得預期的結果時,請使用requests.Session
通常會有所幫助。
問題出在我使用相同的csrf代碼。 需要根據每個請求進行更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.