scrapy專利爬蟲(三)——簡單實際操作
確定鏈接
在chrome中打開審查元素中的network選項,查看查詢專利時發送的請求。觀察後發現在每次查詢的時候,瀏覽器都會先發送兩條請求給服務器。
發送相關請求
經過觀察發現,網站的查詢流程是
- 先發送不帶參數的post請求preExecuteSearch!preExcuteSearch.do將ip地址傳給服務器
- 然後再發送biaogejsAC!executeCommandSearchUnLogin.do將查詢參數發給服務器
填寫表單,發送請求
這裏只給出一個簡單的例子,具體實現見github或代碼附件
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
searchExp = SearchService.getCnSearchExp(self.startDate, proposer, inventor, type)
formData = {
"searchCondition.searchExp": searchExp,
"searchCondition.dbId": "VDB",
"searchCondition.searchType": "Sino_foreign",
"searchCondition.power": "false",
"wee.bizlog.modulelevel": "0200201",
"resultPagination.limit": BaseConfig.CRAWLER_SPEED
}
yield FormRequest(
url="http://www.pss-system.gov.cn/sipopublicsearch/patentsearch/biaogejsAC!executeCommandSearchUnLogin.do",
callback=self.parsePatentList,
method="POST",
headers=headers,
formdata=formData,
meta={
'searchExp': searchExp,
'inventionType': type,
'startDate': self.startDate,
'proposer': proposer,
'inventor': inventor
}
)
數據解析
通過觀察chrome的Element,可以逐個找出我們所需要的元素,例如:
本工程使用beautifulsoup進行解析,對於帶class的元素,使用find(attrs={"class": "className"})
的方法採集即可,其他參數也類似。這裏提供簡單的例子
itemSoup = BeautifulSoup(item.prettify(), "lxml")
header = itemSoup.find(attrs={"class": "item-header"})
pi['name'] = header.find("h1").get_text(strip=True)
pi['type'] = header.find(attrs={"class": "btn-group left clear"}).get_text(strip=True)
pi['patentType'] = QueryInfo.inventionTypeToString(type)
content = itemSoup.find(attrs={"class": "item-content-body left"})
數據收集
同樣的需要對item使用yield,然後將數據傳入pipeline中進行處理,關於更多數據處理的詳細內容將會在下節內容中介紹。
源碼下載
讚賞 | |
微信 | 支付寶 |