无法进行网页抓取,因为找不到表单元素

时间:2021-07-13 12:38:27

标签: web-scraping web-crawler

尝试抓取以下网站: https://israeldrugs.health.gov.il/#!/byDrug

您需要在表单中输入搜索词,然后按左侧的蓝色按钮。

但是,bs4 失败了,因为它找不到表单元素。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

此站点上的数据是使用 javascript 动态加载的。如果您深入研究 XHR(使用浏览器中的“开发人员”选项卡),您将看到这些信息是如何加载到页面中的。顺便说一句,以下假设您使用的是 python;如果没有,您将不得不在另一种语言中找到对应的语言。

import requests
import json

target = 'ATORVASTATIN AS CALCIUM' #this is just a random drug from their list
data = '{"val":"'+target+'","prescription":false,"healthServices":false,"pageIndex":1,"orderBy":0}'


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'en-US,en;q=0.5',
    'Content-Type': 'application/json',
    'Origin': 'https://israeldrugs.health.gov.il',
    'Connection': 'keep-alive',
    'Referer': 'https://israeldrugs.health.gov.il/',
}

response = requests.post('https://israeldrugs.health.gov.il/GovServiceList/IDRServer/SearchByName', headers=headers,  data=data)

#load the json response
meds = json.loads(response.text)
#a random item from the 8th (random, again) drug in the response
meds['results'][7]['dragHebName']

输出:

'טורבה 10'