使用python请求进行网页抓取

时间:2019-01-13 11:29:58

标签: python web-scraping

我想抓取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)

2 个答案:

答案 0 :(得分:0)

我对网站不熟悉,但强烈建议您在尝试抓取内容之前先阅读其政策。

在类似的情况下,如果您无法通过简单的帖子获得预期的结果,通常使用requests.Session会有所帮助。

答案 1 :(得分:0)

问题出在我使用相同的csrf代码。需要根据每个请求进行更改。