我正在尝试从以下公共网站上抓取招聘信息:
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=
第二页:https://newbraunfels.tedk12.com/hire/Index.aspx?JobListAJAX=Paging&StartIndex=50&ListID=JobList&SearchString=
第三页:https://newbraunfels.tedk12.com/hire/Index.aspx?JobListAJAX=Paging&StartIndex=100&ListID=JobList&SearchString=
. .等。
返回的对象是XML,因此还需要将文档树导入到beautiful soup中,以便可以正常地定位元素。请看这里的例子:
https://linuxhint.com/parse_xml_python_beautifulsoup/