发起人:Robot 管理大师

回复 ( 1 )

  1. 深海鱼
    理由
    举报 取消

    这网站真是奇葩,在post中填2460,一次就获取所有数据。

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Author: LoveNight
    # @Date:   2015-11-30 20:19:57
    # @Last Modified by:   LoveNight
    # @Last Modified time: 2015-11-30 21:00:23
    
    import requests
    import csv
    from bs4 import BeautifulSoup as BS
    
    queryUrl = r'http://www.chinapesticide.gov.cn/myquery/queryselect'
    postData = {
        "nylb": "卫生杀虫剂", "pageSize": "2460", "pageNo": "1",
        "djzh":"", "cjmc":"", "sf":"", "zhl":"", "jx":"",
        "zwmc":"", "fzdx":"", "yxcf":"", "yxcf_en":"",
        "yxcfhl":"", "yxcf2":"", "yxcf2_en":"", "yxcf2hl":"",
        "yxcf3":"", "yxcf3_en":"", "yxcf3hl":"", "yxqs_start":"",
        "yxqs_end":"", "yxjz_start":"", "yxjz_end":"",
    }
    
    with open(r"F:\result.csv", 'w', encoding='gbk', newline='') as f:
        writer = csv.writer(f)
        html = requests.post(queryUrl, data=postData).text
        trs = BS(html, "html.parser").find("table", id="tab").find_all("tr")
        for tr in trs:
            row = [x.getText().strip() for x in tr.find_all("td")]
            writer.writerow(row)
    

我来回答

Captcha 点击图片更换验证码