基本信息
源码名称:C# 通过正则采集某网站的图片数据
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2017-11-03
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace 正则提取
{
class Program
{
private static HttpWebRequest myHttpWebRequest;
static void Main(string[] args)
{
//采集部分
Console.WriteLine("编号");
int t = int.Parse(Console.ReadLine());
Console.WriteLine("开始页");
int i = int.Parse(Console.ReadLine());
Console.WriteLine("结束页");
int n = int.Parse(Console.ReadLine());
for (int x = i; i < n; i )
{
string strURL = "https://bbs.xd.com/thread-" t "-" i "-1.html";
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.Timeout = 20 * 1000; //连接超时
myHttpWebRequest.Accept = "text / html,application / xhtml xml,application / xml; q = 0.9,*/*;q=0.8";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
//myHttpWebRequest.Headers.Add("Cookie", "UM_distinctid = 15b917f117652c - 0e0f802cc2344a8 - 13646e4a - 1fa400 - 15b917f117751c; CNZZDATA1254852923 = 818605592 - 1492791763 - http% 253A % 252F % 252Fsgk.fbisb.com % 252F % 7C1492850579");
//myHttpWebRequest.Host = "s.70sec.com";
//myHttpWebRequest.Referer = "http://s.70sec.com/";
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
StreamReader myreader = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.UTF8);
string responseText = myreader.ReadToEnd();
string[] lines = responseText.Split('\n');//new added
myHttpWebResponse.Close();
myreader.Close();
//提取部分
foreach (string line in lines)
// while ((line = sr.ReadLine()) != null)//修改处
{
int index = line.IndexOf("static/image/siteicon/v4/xd.png");
if (index > -1)
{
// Console.WriteLine(line);
Match mc = Regex.Match(line, @"(?<=nowrap;""> )(.*?)(?=<img class=""uc_platform_icon"" src=""static/image/siteicon/v4/xd.png)", RegexOptions.Multiline);
string href1 = mc.Groups[1].Value;
Console.WriteLine("{1}获取到内容{0}:", x,href1);
string href = href1 "\r\n";
StreamWriter sw = File.AppendText("1.txt");
sw.Write(href);
sw.Close();
}
}
}
}
}
}