python农产品集购网获取现货商城信息

本文介绍了一种使用Python爬虫技术从农产品集购网站抓取现货商城详细信息的方法。通过多进程并行处理提高效率,收集了包括产品名称、规格、品牌等在内的多种属性,并将数据整理成Excel文件。
Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

# -*- coding: utf-8 -*-
"""
Created on Wed Feb  7 23:33:26 2018

@author: Administrator
"""
'''
农产品集购网获取现货商城信息
'''
import requests
from bs4 import BeautifulSoup
import time
from multiprocessing import Pool

headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}

#url='http://www.16988.com/fronts/spotmall/0/0/0/0/0/0/0/3/0?searchKeyword=&searchBrand=&is_odd=&searchCompany='
total=[]
def get_nongchanpin(url):
    res=requests.get(url,headers=headers)
    print(res.status_code)
    soup=BeautifulSoup(res.text,'html.parser')
    all_chanpin=soup.find_all('ul',class_='foreach-table-tr d-hover-border ')
    for each in all_chanpin:
        pinming=each.find('li',class_='productname').text
        guige=each.find('li',class_='pecifications').text
        pinpai=each.find('li',class_='brands').text
        cangku=each.find('li',class_='warehouse').text
        try:
            qidingliang=int(each.find('li',class_='orderquantity').text)
        except ValueError:
            qidingliang=float(each.find('li',class_='orderquantity').text)
        try:
            chicangliang=int(each.find('li',class_='positions').text)
        except ValueError:
            chicangliang=float(each.find('li',class_='positions').text)
        zoushi=each.find('li',class_='foreach-pricetrend pricetrend').font.text
        zoushi_jiantou=each.find('li',class_='foreach-pricetrend pricetrend').img['src']
        jiage=each.find('li',class_='foreach-price price').text.strip()
        info={'品名':pinming,
              '规格':guige,
              '品牌':pinpai,
              '仓库':cangku,
              '起订量(吨)':qidingliang,
              '持仓量(吨)':chicangliang,
              '走势':zoushi,
              '箭头':zoushi_jiantou,
              '价格(元/吨)':jiage}
        total.append(info)
    return total

if __name__ == '__main__':
    for i in range(501,967):
        url='http://www.16988.com/fronts/spotmall/0/0/0/0/0/0/0/'+str(i)+'/0?searchKeyword=&searchBrand=&is_odd=&searchCompany='
        get_nongchanpin(url)
        time.sleep(0.2)
'''
if __name__ == '__main__':
        urls=['http://www.16988.com/fronts/spotmall/0/0/0/0/0/0/0/{}/0?searchKeyword=&searchBrand=&is_odd=&searchCompany='.format(i) for i in range(0,5)]
        start=time.time()
        pool=Pool()
        pool.map(get_nongchanpin,urls)
        end=time.time()
        print('时间:',end-start)
'''        
import pandas as pd
df=pd.DataFrame(total)
df.to_excel('nongzuowu2.xls',columns=['品名','规格','品牌','仓库','起订量(吨)','持仓量(吨)','走势','箭头','价格(元/吨)'])

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值