慕侠2389804
该页面上的数据是使用 JavaScript 动态加载的,因此您不能仅通过该页面的请求来获取它。使用浏览器中的 Developer 标签,您可以拦截生成数据的 xhr 并将其用于请求。这就是本例中的情况。import requestsheaders = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0', 'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.5', 'X-Requested-With': 'XMLHttpRequest', 'Connection': 'keep-alive', 'Referer': 'https://search.aap.org/nicu/',}params = ( ('Model', 'X.PagedList.StaticPagedList`1[AAP.Website.Search.NICU.Web.ViewModels.NicuVm]'), ('jsonSearchVmString', '{"SearchCriteria":{"Name":null,"City":null,"State":null,"Zip":null,"Level":2,"LevelAssigner":null,"BedNumberRange":0},"States":null,"Levels":null,"LevelAssigners":null,"BedNumberRangeList":null,"NumberOfPages":0}'), ('page', '2'), ('class', 'blue-pager'), ('X-Requested-With', 'XMLHttpRequest'), ('_', '1603318486561'),)response = requests.get('https://search.aap.org/nicu/', headers=headers, params=params)参数之一是('page', '2');如果您将请求包装在for适当范围内的循环中,您应该能够requests.get()在所有页面上执行此操作。
蛊毒传说
在这种情况下,您不必使用 POST 请求。如果单击下一页时查看 URL,您将看到可以更改的所有参数。此外,要获取所有页面,您可以解析 HTML 并查找<a rel="next">标签。如果标签不存在,则您位于最后一页(尝试将参数更改"LevelAssigner"为3其他值):import requestsfrom bs4 import BeautifulSoupurl = 'https://search.aap.org/nicu/?Model=X.PagedList.StaticPagedList`1[AAP.Website.Search.NICU.Web.ViewModels.NicuVm]&jsonSearchVmString={"SearchCriteria":{"Name":null,"City":null,"State":null,"Zip":null,"Level":0,"LevelAssigner":"3","BedNumberRange":0},"States":null,"Levels":null,"LevelAssigners":null,"BedNumberRangeList":null,"NumberOfPages":0}&class=blue-pager&X-Requested-With=XMLHttpRequest&page='page = 1while True: print('Page {}..'.format(page)) soup = BeautifulSoup(requests.get(url + str(page)).content, 'html.parser') for row in soup.select('.row.font-700'): print(row.get_text(strip=True, separator=' ')) next_page = soup.select_one('a[rel="next"]') if not next_page: break page += 1印刷:Page 1..ANDERSON REGIONAL MEDICAL CENTER Meridian, MS 39301Ann & Robert H. Lurie Children's Hospital of Chicago Chicago, IL 60611BAYSTATE MEDICAL CENTER Springfield, MA 01199Beacon Children's Hospital South Bend, IN 46601-1033BEN TAUB GENERAL HOSPITAL Houston, TX 77030Page 2..BETH ISRAEL DEACONESS MEDICAL CENTER Boston, MA 02215BRIGHAM AND WOMEN'S HOSPITAL Boston, MA 02115CHILDREN'S HOSPITAL OF NEW ORLEANS New Orleans, LA 70118CLEVELAND CLINIC formally MERIDIA HILLCREST HOSPITAL Mayfield Heights, OH 44124EAST JEFFERSON GENERAL HOSPITAL Metairie, LA 70006Page 3..GEORGETOWN UNIVERSITY HOSPITAL Washington, DC 20007GREENWICH HOSPITAL Greenwich, CT 06830HELEN DEVOS CHILDREN'S HOSPITAL Grand Rapids, MI 49503INOVA LOUDOUN HOSPITAL Leesburg, VA 20176JACOBI MEDICAL CENTER Bronx, NY 10461Page 4..LAKEVIEW REGIONAL MEDICAL CENTER HOSPITAL Covington, LA 70433LOYOLA UNIVERSITY MEDICAL CENTER Maywood, IL 60153MELROSE WAKEFIELD HOSPITAL Melrose, MA 02176MERCYHEALTH HOSPITAL ROCKTON AVENUE CAMPUS formally ROCKFORD HEALTH SYSTEM Rockford, IL 61103RUSH FOUNDATION HOSPITAL Meridian, MS 39301Page 5..Shetal Shah Valhalla, NY 10595SLIDELL MEMORIAL HOSPITAL Slidell, LA 70458SOUTHERN NEW HAMPSHIRE MEDICAL CENTER Nashua, NH 03061St Mary Medical Center Apple Valley, CA 92307ST. AGNES HOSPITAL Baltimore, MD 21229Page 6..THE CHILDREN'S HOSPITAL AT NAVICENT HEALTH Macon, GA 31208-6000TOURO INFIRMARY New Orleans, LA 70115TULANE LAKESIDE HOSPITAL Metairie, LA 70001UNIVERSITY MEDICAL CENTER - SUNY STONY BROOK Stony Brook, NY 11794-8111UNIVERSITY OF IOWA CHILDREN'S HOSPITAL Iowa City, IA 52242Page 7..UNIVERSITY OF LOUISVILLE SCHOOL OF MEDICINE Louisville, KY 40232-5070UNIVERSITY OF MISSISSIPPI MEDICAL CENTER Jackson, MS 39216-4505UNIVERSITY OF ROCHESTER MEDICAL CENTER Rochester, NY 14642UNIVERSITY OF WASHINGTON MEDICAL CENTER Seattle, WA 98195WakeMed Cary Hospital Cary, NC 27518Page 8..WakeMed North Family Health & Women’s Hospital Raleigh, NC 27614WakeMed Raleigh Hospital Raleigh, NC 27610Wellstar Kennestone Hospital NICU Marietta, GA 30066WILLIS KNIGHTON CENTER FOR WOMEN'S HEALTH Shreveport, LA 71118WOMAN'S HOSPITAL Baton Rouge, LA 70817