python爬虫——爬取拉勾上的职位信息

爬取拉勾网站岗位数据

1、调用网页

查找网页链接规律
在这里插入图片描述
写一个for循环,爬取每一个网页的职位信息

def down():
    for i in range(1,4):
        if i == 1:
            strUrl = "http://72.itmc.org.cn/JS001/open/show/zhaopin/index.html"
        else:
            strUrl ="http://72.itmc.org.cn/JS001/open/show/zhaopin/index_" + str(i) + ".html"
        print(strUrl)

2、找到网页节点

在这里插入图片描述
网页源代码里面的 li 标签对已经包含了每个岗位的职位名称、发布公司、薪酬等,现在只需要用BeautifulSoup的find功能找到所有的 li 节点

def parserWebContent(content):
    bs = BeautifulSoup(content,"html.parser")
    dicts = {
        "class":"item_con_list",
        "style" : "display:block"
    }
    ulTag = bs.find("ul",dicts)
    #print(ulTag)
    liTags = ulTag.find_all("li")
    #print(len(liTags))
    for liTag in liTags:
        parseProductTag(liTag)

3、取出需要的职位信息

def parseProductTag(productTag):
    index = 0 
    for subTag in productTag.div.contents:
        #print(index)
        #print(subTag)
        pass #以上是调试代码
    name = productTag["data-positionname"]
    salary = productTag["data-salary"]
    company = productTag["data-company"]
    print(name,salary,company)

采用直接取出
【data-positionname】职位名称
【data-salary】工资待遇
【data-company】招聘公司

4、全部代码

import requests
from bs4 import BeautifulSoup

heads = {
    "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
#模拟用户登录,防止反爬虫

def parseProductTag(productTag):
    index = 0
    for subTag in productTag.div.contents:
        #print(index)
        #print(subTag)
        pass
    name = productTag["data-positionname"]
    salary = productTag["data-salary"]
    company = productTag["data-company"]
    print(name,salary,company)
    #写入文件
    f = open("rencaiwang.txt",mode="a+",encoding="utf-8")
    f.write(name + ";" + salary + ";" + company +"\n")
    f.close()

def parserWebContent(content):
    bs = BeautifulSoup(content,"html.parser")
    dicts = {
        "class":"item_con_list",
        "style" : "display:block"
    }
    ulTag = bs.find("ul",dicts)
    #print(ulTag)
    liTags = ulTag.find_all("li")
    #print(len(liTags))
    for liTag in liTags:
        parseProductTag(liTag)


def downWebContent(url):
    r = requests.get(url,headers = heads)
    print(r.status_code)
    r.encoding = r.apparent_encoding
    #print(r.text)
    parserWebContent(r.text)


def down():
    for i in range(1,4):
        if i == 1:
            strUrl = "http://72.itmc.org.cn/JS001/open/show/zhaopin/index.html"
        else:
            strUrl ="http://72.itmc.org.cn/JS001/open/show/zhaopin/index_" + str(i) + ".html"
        print(strUrl)
        downWebContent(strUrl)
        break

down()#调用上面定义的函数
### 使用Python编写爬虫抓取优酷视频 #### 抓取目标与工具准备 为了实现对优酷视频的数据获取,通常会涉及到网页请求、解析HTML文档以及处理API接口返回的内容。对于这类任务,`requests`库用于发起HTTP/HTTPS网络请求;而`BeautifulSoup`或`lxml`则适合于解析HTML/XML结构化数据。如果遇到JavaScript渲染的页面,则可能需要用到Selenium这样的浏览器自动化框架[^1]。 #### 处理登录验证机制 部分网站可能会设置较为严格的反扒措施,比如验证码识别或是基于Cookies的身份认证流程。针对这种情况,在开发初期就要考虑好应对策略,例如模拟真实用户的浏览行为模式,定期更换User-Agent字符串,甚至利用第三方服务绕过图形验证码挑战等问题[^2]。 #### 数据提取逻辑设计 当面对像优酷这样大型综合性媒体平台时,直接从公开可访问的部分入手往往是最简单有效的方式之一。具体来说就是先定位到含有目标资源列表页URL模板,再依据一定规则遍历每一页直至结束条件满足为止。与此同时还要注意观察详情页链接构造规律以便后续深入挖掘更多有价值的信息项[^3]。 ```python import requests from bs4 import BeautifulSoup def fetch_video_list(page_num=1): url = f"https://www.youku.com/v_olist/c_97_p_{page_num}.html" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', } response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, "html.parser") video_items = [] for item in soup.select('.v-link'): title = item['title'] href = item['href'] video_items.append({ 'title': title, 'url': href }) return video_items if __name__ == "__main__": videos = fetch_video_list() for v in videos[:5]: print(f"{v['title']}: {v['url']}") ``` 此段代码仅作为基础示范用途,请勿滥用本技术手段非法采集他人受版权保护的作品!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值