运行我的刮刀我可以看到它除了链接到我需要的每个学校之外的其他链接。不过,我已经创建了正确的xpath。该站点包含惰性加载方法。也许需要抓住json的回应。我尝试过:
import requests
from lxml import html
url = "http://www.boarding.org.au/find-a-school"
def LazyLoadWeb(address):
try :
page = requests.get(address, timeout=30)
except Exception:
print('timed out')
else:
tree = html.fromstring(page.text)
titles = tree.xpath('//div[contains(@class,"clearfix")]')
for title in titles:
links=title.xpath('.//a/@href')
for link in links:
print(link)
LazyLoadWeb(url)
答案 0 :(得分:2)
你对json的反应是正确的。该站点使用Ajax填充内容。您需要发出一个帖子请求,只需从响应中解析json。
import requests
url = 'http://www.boarding.org.au/ajax-calls/GetSchoolsJson'
payload = {"state": 'null', "schoolType": 2, "orderMode": "ASC", "enableSchoolType": 'false', "loadAll": 'true'}
req = requests.post(url, json=payload)
data = req.json()
for i, item in enumerate(data, start=1):
print(i, item['URL'])
# 1 /schools/details/4/Abbotsleigh
# ...
# 189 /schools/details/83/Yirara-College