爬虫抓取51job软件测试页面上 某公司名称 python实现

#需求:抓取51job软件测试页面上 某公司名称
主要用来学习调用父类

spider1.py

import requests
class spider():
    def __init__(self):
        self.headers={"user-agent":"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"}
    def submit(self,url):
        response=requests.get(url,self.headers)
        response.encoding = "gbk"
        return response.text

if __name__=="__main__":
    obj=spider()
    res=obj.submit("https://www.51job.com")
    print(res)

spider2.py
spider2里只在初始化方法里加了url参数,只为学习调用父类使用,将url直接写在spider1的初始化里更为合适

#51job网页 搜索软件测试的界面,用子类调用父类的属性和方法
import requests
from Spider.spiderV1 import spider

class spiderV2(spider):
    def __init__(self,url):  #--url是变量
        self.url2=url         #--self.url2 是属性
        spider.__init__(self)

if __name__=='__main__':
    u="https://search.51job.com/list/080300,000000,0000,00,9,99,%25E8%25BD%25AF%25E4%25BB%25B6%25E6%25B5%258B%25E8%25AF%2595,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="
    obj=spiderV2(u)      #--u 是变量
    res=obj.submit(obj.url2)     #-- url2是属性   **对象后面可以点属性/方法,不能点变量
    print(res)

spider3.py

import requests
from lxml import etree
from Spider.spiderV2 import spiderV2

class spiderV3(spiderV2):
    def __init__(self):
        spiderV2.__init__(self,u)
    def position(self,spage):
        dic={}
        document=etree.HTML(spage)
        ele=document.xpath('//*[@id="resultList"]/div[4]/span[1]/a/text()')
        dic=['公司名:',ele]
        return dic
    def savedata(self,d):
        with open("company.txt","w") as file:
            file.write(str(d))

if __name__=="__main__":
    u="https://search.51job.com/list/080300,000000,0000,00,9,99,%25E8%25BD%25AF%25E4%25BB%25B6%25E6%25B5%258B%25E8%25AF%2595,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="
    obj3=spiderV3()
    obj3.url3=u
    res=obj3.submit(obj3.url3)
    pos=obj3.position(res)
    print(pos)
    obj3.savedata(pos)

你可能感兴趣的:(爬虫)