📜  使用Python获取最新的政府工作信息

📅  最后修改于: 2022-05-13 01:54:20.960000             🧑  作者: Mango

使用Python获取最新的政府工作信息

先决条件: Python GUI – tkinter  

在这些文章中,我们将编写Python脚本来获取最新的政府工作信息。

需要的模块

  • BeautifulSoup Beautiful Soup(bs4) 是一个Python库,用于从 HTML 和 XML 文件中提取数据。这个模块没有内置于Python中。要安装此类型,请在终端中输入以下命令。
pip install bs4
  • requests Requests 允许您非常轻松地发送 HTTP/1.1 请求。这个模块也没有内置于Python中。要安装此类型,请在终端中输入以下命令。
pip install requests

方法:

  • 提取给定 URL 的数据形式
  • 在请求和 Beautiful Soup 的帮助下抓取数据
  • 将该数据转换为 HTML 代码。
  • 找到所需的详细信息并过滤它们。

我们来看看脚本的逐步执行

第一步:导入所有依赖

Python3
import requests 
from bs4 import BeautifulSoup


Python3
def getdata(url): 
    r = requests.get(url) 
    return r.text


Python3
# import module
import requests
import pandas as pd
from bs4 import BeautifulSoup
  
  
res = ''
  
# link for extract html data
def getdata(url):
    r = requests.get(url)
    return r.text
  
htmldata = getdata("https://www.sarkariresult.com/latestjob.php")
soup = BeautifulSoup(htmldata, 'html.parser')
  
for li in soup.find_all("div", id="post"):
    res += (li.get_text())
  
print(res)


Python3
# import module
import requests
import pandas as pd
from bs4 import BeautifulSoup
from tkinter import *
from tkinter import messagebox
  
res = []
  
  
def getdata(url):
    r = requests.get(url)
    return r.text
  
  
def getinfo():
    result = ''
    htmldata = getdata("https://www.sarkariresult.com/latestjob.php")
    soup = BeautifulSoup(htmldata, 'html.parser')
      
    for li in soup.find_all("div", id="post"):
        result += (li.get_text())
    res.set(result)
  
  
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
  
# Variable Classes in tkinter
res = StringVar()
  
# Creating label for each information
# name using widget Label
Label(master, text="List of the Jobs :",
      bg="light grey", font="100").grid(row=0, sticky=W)
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="light grey").grid(
    row=3, column=1, sticky=W)
  
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Get latest job", command=getinfo)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
  
mainloop()


第 2 步:创建 URL 获取函数

蟒蛇3

def getdata(url): 
    r = requests.get(url) 
    return r.text

第 3 步:现在将 URL 传递到 getdata函数并将该数据转换为 HTML 代码

蟒蛇3

# import module
import requests
import pandas as pd
from bs4 import BeautifulSoup
  
  
res = ''
  
# link for extract html data
def getdata(url):
    r = requests.get(url)
    return r.text
  
htmldata = getdata("https://www.sarkariresult.com/latestjob.php")
soup = BeautifulSoup(htmldata, 'html.parser')
  
for li in soup.find_all("div", id="post"):
    res += (li.get_text())
  
print(res)

输出:

使用Tkinter申请最新的工作信息此脚本将上述实现实现到 GUI 中。

蟒蛇3

# import module
import requests
import pandas as pd
from bs4 import BeautifulSoup
from tkinter import *
from tkinter import messagebox
  
res = []
  
  
def getdata(url):
    r = requests.get(url)
    return r.text
  
  
def getinfo():
    result = ''
    htmldata = getdata("https://www.sarkariresult.com/latestjob.php")
    soup = BeautifulSoup(htmldata, 'html.parser')
      
    for li in soup.find_all("div", id="post"):
        result += (li.get_text())
    res.set(result)
  
  
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
  
# Variable Classes in tkinter
res = StringVar()
  
# Creating label for each information
# name using widget Label
Label(master, text="List of the Jobs :",
      bg="light grey", font="100").grid(row=0, sticky=W)
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="light grey").grid(
    row=3, column=1, sticky=W)
  
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Get latest job", command=getinfo)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
  
mainloop()

输出: