Python爬取猪八戒网站

本文介绍了一种使用Python爬虫技术抓取猪八戒网上托管任务需求的方法,通过发送GET请求并解析网页结构,成功获取了包括发布时间、参与人数、产品名称等在内的详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

爬取猪八戒网站,方便你接单;
1.下面是猪八戒为托管的单的链接

url="https://task.zbj.com/?s=2&so=2&ss=0" #为托管的连接
url_2="https://task.zbj.com/page2.html?s=2&so=2&ss=0" #第二页的连接

2.第一步想网页发送get请求,解析网页的结构。
在这里插入图片描述
获取需求的所有的网页标签。

demands=select.xpath('//*[@id="utopia_widget_7"]/div[@class="demand-list"]//div[@class="demand-card"]')
    print("总计有%d条发布信息."%len(demands))

3.根据路径写获取信息的方法,不过有太多的换行,再改正一下。

在这里插入图片描述
4.加上了这个之后去除换行,
在这里插入图片描述

# -*- coding: utf-8 -*-
# @Time    : 2020/3/22 15:47
# @Author  : 结尾!!
# @FileName: spider_task_zbj.py
# @Software: PyCharm

import requests



#使用xpath
from scrapy.selector import Selector
header={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
}
def parse_zbj(html_url):
    resp=requests.get(html_url,headers=header)
    resp.encoding="utf-8"
    print("网页返回的状态码:",resp.status_code)
    select=Selector(resp)
    demands=select.xpath('//*[@id="utopia_widget_7"]/div[@class="demand-list"]//div[@class="demand-card"]')
    print("总计有%d条发布信息."%len(demands))
    for div in demands:
        release_time=div.xpath('./div[@class="demand-card-head"]/span[1]/text()').extract_first().strip() #发布时间
        attend_num=div.xpath('./div[@class="demand-card-head"]/span[2]/text()').extract_first().strip() #参与人数
        title=div.xpath('./div[@class="demand-card-body"]/@title').extract_first().strip() #产品名称
        price=div.xpath('.//div[@class="demand-price"]/text()').extract_first().strip() #价格
        describe=div.xpath('.//div[@class="demand-card-desc"]/text()').extract_first().strip() #产品描述
        commodity=div.xpath('./div[@class="demand-foot-tags flt"]/span/text()').extract_first() #招标方
        link=div.xpath('.//a[@class="prevent-defalut-link"]/@href').extract_first() #需求的连接
        print([release_time,attend_num,title,price,describe,commodity,link])
        line=[release_time, attend_num, title, price, describe, commodity, link]
        save_csv(line)

    return
import csv
from datetime import datetime
now = datetime.now()
def save_csv(lise_line):
    file = csv.writer(open("./2020-3-22-17-00.csv",'a',newline="",encoding="utf-8"))
    file.writerow(lise_line)

if __name__ == '__main__':
    url = "https://task.zbj.com/?s=2&so=2&ss=0"  # 为托管的连接
    url_2 = "https://task.zbj.com/page2.html?s=2&so=2&ss=0"  # 第二页的连接
    parse_zbj(url)


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43351935

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值