读淘宝页面字节流提取宝贝图片地址宝贝标题宝贝价格

public static class taobao_message
        {
            /// <summary>
            /// 读页面的byte转化为string
            /// </summary>
            /// <param name="url">地址</param>
            /// <returns></returns>
            public static string webclinet_content(string url)
            {
                url = url.Replace("http://", "");
                System.Net.WebClient client = new WebClient();
                byte[] page = client.DownloadData("http://" + url);  
                string content = System.Text.Encoding.GetEncoding("GB2312").GetString(page);//淘宝的页面编码为gb2312
                return content;
            }
            /// <summary>
            /// 读淘宝宝贝的信息
            /// 数组内容{宝贝图片地址,宝贝标题,宝贝价格}
            /// </summary>
            /// <param name="url">宝贝地址</param>
            /// <returns></returns>
            public static string[] baobei_mess(string url)
            {
                string content = webclinet_content(url);
                string baobei_img =get_taobao(content,1);
                string baobei_title = get_taobao(content,2);
                string baobei_price = get_taobao(content,3);
                string[] arry1 = {baobei_img,baobei_title,baobei_price};
                return arry1;;
            }
            /// <summary>
            /// 特定标签内容提取
            /// </summary>
            /// <param name="content">提取的字符串</param>
            /// <param name="type">验证类型0:空; 1:淘宝贝图片;2:宝贝标题;3:宝贝价格;</param>
            /// <returns></returns>
            public static string get_taobao(string content, int type)
            {
                string result = "";
                string reg = "";
                switch (type)
                {
                    case 0: return "";
                    case 1: reg = @"J_ImgBooth\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>"; break;
                    case 2: reg = "<h3>(<a[^>]*>)?([^<]*)(</a>)?</h3>"; break;
                    case 3: reg = "J_StrPrice[^>]*>([^<>]*)(</)"; break;
                }
                string regex = reg;
                Regex re = new Regex(regex);
                MatchCollection matches = re.Matches(content);
                System.Collections.IEnumerator enu = matches.GetEnumerator();
                switch (type)
                {
                    case 0: return "";
                    case 1:
                        while (enu.MoveNext() && enu.Current != null)
                        {
                            Match match = (Match)(enu.Current);
                            result += match.Groups["imgUrl"];
                        } break;
                    case 2:
                        while (enu.MoveNext() && enu.Current != null)
                        {
                            Match match = (Match)(enu.Current);
                            result += match.Groups[2];
                        } break;
                    case 3:
                        while (enu.MoveNext() && enu.Current != null)
                        {
                            Match match = (Match)(enu.Current);
                            result += match.Groups[1];
                        } break;
                }
                return result;
           }
        }
在PHP中,我们可以使用GD库来实现将二进制字节流转换为二维码图片并将其显示在页面上。 首先,我们需要确保服务器上已经安装了GD库。可以通过检查phpinfo()函数的输出来验证。 接下来,我们需要使用一个PHP二维码生成库,例如ZXing或phpQRCode。这些库提供了一些函数和方法来生成和处理二维码。 首先,我们需要从二进制字节流中创建一个图像资源。可以使用imagecreatefromstring()函数将字节流转换为图像资源。 然后,我们可以使用库中提供的函数或方法将图像资源转换为二维码。这通常涉及设置二维码的内容、大小和错误纠正级别等信息。具体的方法和函数可能会因所使用的库而有所不同。 一旦我们生成了二维码图像,我们可以使用imagepng()或imagejpeg()函数将其保存为PNG或JPEG格式的图像文件。 最后,我们可以将生成的二维码图像直接输出到浏览器,以便在页面上显示。可以使用header()函数设置适当的Content-Type头,然后使用imagepng()或imagejpeg()函数将图像输出到浏览器。 以下是一个简单的例子,演示如何使用phpQRCode库将二进制字节流转换为二维码并在页面上显示: ```php <?php // 引入phpQRCode库 require_once 'phpqrcode/qrlib.php'; // 二进制字节流 $byteStream = ... // 从其他地方获取二进制字节流 // 创建图像资源 $image = imagecreatefromstring($byteStream); // 设置二维码内容和大小 $text = 'http://example.com'; $size = 5; // 创建二维码 QRcode::png($text, false, QR_ECLEVEL_L, $size, 2); // 输出到浏览器 header('Content-Type: image/png'); imagepng($image); imagedestroy($image); ?> ``` 在上面的例子中,我们通过引入phpQRCode库将二进制字节流转换为图像资源,然后使用QRcode类的png()方法创建二维码。最后,我们将生成的二维码图像输出到浏览器。请注意,你需要将phpqrcode文件夹中的库文件正确放置在项目中。 这只是一个简单的示例,实际使用时还需要根据具体需求进行适当的调整和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值