簡體   English   中英

如何使用 Python 抓取具有相同 URL 的 HTML 表中的多個頁面?

[英]How to scrape multiple pages in HTML table with same URL with Python?

我正在嘗試從以下公共網站上抓取招聘信息:

https://newbraunfels.tedk12.com/hire/Index.aspx

我知道這里有一些類似的問題,但我已經關注了所有這些問題,但由於我的 javascript/html 技能有限,我似乎無法弄清楚。

我可以毫無問題地獲得第一頁,但似乎無法訪問以下三頁。

我最好的嘗試如下,但它仍然只返回列表的第一頁:

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get(url).content, "html.parser")


def load_page(soup, page_num):
    payload = {
        "__EVENTTARGET": "",
        "__EVENTARGUMENT": "PageIndexNumber${}".format(page_num),
    }
    for inp in soup.select("input"):
        payload[inp["name"]] = inp.get("value")
    soup = BeautifulSoup(requests.post(url, data=payload).content, "lxml")
    
    return soup


# print hospitals from first page:
for jobs in soup.select("table"):
    print(jobs.text)

# load second page
soup = load_page(soup, 2)
for jobs in soup.select("table"):
    print(jobs.text)

提前謝謝你。

在這種情況下,一種更簡單的方法可能是使用 get 變量直接查詢每個頁面。 “StartIndex”變量應該是 50 的倍數,因為每頁顯示 50 個結果。 對於要抓取的每一頁結果,只需將其增加 50。

第 1 頁: https ://newbraunfels.tedk12.com/hire/Index.aspx?JobListAJAX=Paging&StartIndex=0&ListID=JobList&SearchString=

第 2 頁: https ://newbraunfels.tedk12.com/hire/Index.aspx?JobListAJAX=Paging&StartIndex=50&ListID=JobList&SearchString=

第 3 頁: https ://newbraunfels.tedk12.com/hire/Index.aspx?JobListAJAX=Paging&StartIndex=100&ListID=JobList&SearchString=

..ETC。

返回的對象是 XML,因此您還需要將文檔樹導入到 beautiful soup 中,以便您可以正常定位元素。 請參閱此處的示例:

https://linuxhint.com/parse_xml_python_beautifulsoup/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM