正則表達式 取出img标签 保存于指定路径

时间:2023-11-13 19:14:32

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net;

using System.IO;

using System.Text;

using System.Text.RegularExpressions;

namespace DloadPic

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            string allstr = "<p dir=\"ltr\"><b>首先小编提醒广大车友们,下面应急方法都是有前提的:</b></p> <p dir=\"ltr\">  <b>1、你没有影响他人通过。没横在人行道上,没挡住了其它车辆。

</b></p> <p dir=\"ltr\"><b>  2、你没有逆向停车。

</b></p> <p dir=\"ltr\">";

            allstr += "<img src=\"http://mmbiz.qpic.cn/mmbiz/1AduwlOjUXlYnmR39eBSwp50G5SKib3hPAjbkWnMibSyg3tu5e54TRF1hVyzwWr0BdEgQh9WwpwhkVQ5apeibMV1Q/640?wx_fmt=jpeg&wxfrom=5&tp=webp\"><br></p>
<p dir=\"ltr\">  排除上面这两条后。假设交警正准备对你的停车行为进行";

            allstr += "取证,<b>你仅仅须要迅速拉开车门,回到驾驶位置,正常驶离就能够了。</b>由于证据照片中,驾驶位置上有驾驶员的,是不能作为“机动车违反规则停放且驾驶人不在现场或在现场拒绝马上驶离。妨碍其它车辆、行人通行的”行为进行处罚的。

<br></p> <p dir=\"ltr\">";

            allstr += "<img src=\"http://mmbiz.qpic.cn/mmbiz/1AduwlOjUXlYnmR39eBSwp50G5SKib3hPf1k0Vrx12u3EQqmQEA0icCWV10HzicL2ZkHLaF46D2HbRZryZK39XiaTA/640?

wx_fmt=jpeg&wxfrom=5&tp=webp\"><br></p>
<p dir=\"ltr\">  <b>假设交警正准备给你的车贴条。你能够边打开手";

            allstr += "机的录像功能(最好设置有快捷方式)边告诉交警你暂时停了一下如今立即离开。</b>假设交警仍然执意贴条,那么这段录像就能够作为申诉和复议证据。由于假设驾驶人在场,交警应该是指出违法行为,予以口头警告,令驾驶人立即驶离,驾驶人仅仅要高速驶离就可以。";

            allstr += "</p> <p dir=\"ltr\">  <img src=\"

wx_fmt=jpeg&wxfrom=5&tp=webp\'><br></p">http://mmbiz.qpic.cn/mmbiz/1AduwlOjUXlYnmR39eBSwp50G5SKib3hPtD8FYz6gsOAK3GxHtN6hU9A2ibuHeFJYgXlm6Z1PkbgANiap04BNnueg/640?

wx_fmt=jpeg&wxfrom=5&tp=webp\"><br></p

>
<p dir=\"ltr\">  微群里一小伙伴在医院暂时等人";

            allstr += ",我把车停在路边,降下玻璃等待,后视镜里能够看到一位交警由远及近的一辆一辆的贴条,贴到我的车时候发现驾驶位置有人。人家扭头就走了。

一说明,这位交警非常清楚驾驶员在现场不能处罚;二说明,口头警告对他来说没有不论什么意义。不如节约时间到路对面继续";

            allstr += "贴条,更具有现实价值。

</p> <p dir=\"ltr\">  <img src=\"http://mmbiz.qpic.cn/mmbiz/1AduwlOjUXlYnmR39eBSwp50G5SKib3hPUh33wnDpYG4hhW1Gr9eCqvu9vc0IWluZaBnYTpTicjKvxctARiaRnliag/640?

wx_fmt=jpeg&wxfrom=5&tp=webp\"><br></p>
<p dir=\"ltr\">  朋友";

            allstr += "们。细致阅读交通法。学习交通法,遵守交通法是保证安全,避免处罚的最有效方法。争辩、吵架、送烟、生气、发泄都非常OUT!

效果不好,还解决不了问题。</p> <p dir=\"ltr\">  <img ";

            allstr += "src=\"http://mmbiz.qpic.cn/mmbiz/1AduwlOjUXlYnmR39eBSwp50G5SKib3hPgTUyALQPkTsIIF55ZumuuTcYbGL43lttgBDTFVlrficnbeMwK3O42nQ/640?wx_fmt=jpeg&wxfrom=5&tp=webp\"></p>
<p dir=\"ltr\"><img ";

            allstr += "src=\"

wxfrom=5&wx_fmt=jpeg&tp=webp\'><b><b></b></b></p">http://mmbiz.qpic.cn/mmbiz/DaFXKpOGEkylF3es0sMdI99dbBO3ia0c6olvkGrSoBiaDCiaPlI1kUgdvNrnchmdbg1hmIkvuVwa8OuXgnuLbXevQ/640?wxfrom=5&wx_fmt=jpeg&tp=webp\"><b><b></b></b></p

>";

            Regex reg = new Regex("<img\\s+src=\"(.*?)\"");

           // MatchCollection matches = reg.Matches(allstr);

            allstr = reg.Replace(allstr,new MatchEvaluator(matchreplace));

            string str = allstr;

}

public string matchreplace(Match m_match)

        {

            string m_return = m_match.Groups[0].Value;

            string m_picurl = m_match.Groups[1].Value;

            string temppartsrc = m_picurl.Substring(0, m_picurl.IndexOf('?'));

            string temp = m_picurl.Substring(m_picurl.IndexOf("wx_fmt=") + 7);

            string pictype = temp.Substring(0, temp.IndexOf("&"));

            string src = temppartsrc + "." + pictype;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(src);

            request.AllowAutoRedirect = true;

WebProxy proxy = new WebProxy();

            proxy.BypassProxyOnLocal = true;

            proxy.UseDefaultCredentials = true;

            request.Proxy = proxy;

            WebResponse response = request.GetResponse();

            string virthpath = "";

            using (Stream stream = response.GetResponseStream())

            {

                using (MemoryStream ms = new MemoryStream())

                {

                    Byte[] buffer = new Byte[1024];

                    int current = 0;

                    while ((current = stream.Read(buffer, 0, buffer.Length)) != 0)

                    {

                        ms.Write(buffer, 0, current);

                    }

                    byte[] arry = ms.ToArray();

                     virthpath = "./image/" + DateTime.Now.ToString("yyyyMMddhhmmss") + "." + pictype;

                    string path = Server.MapPath(virthpath);

                    File.WriteAllBytes(path, arry);

                }

            }

            m_return = m_return.Replace(m_picurl, virthpath);

            return m_return;

        }

}

}