日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
用C編程從網頁數據庫中提取內容(c編程提取網頁數據庫中內容)

用C語言編程從網頁數據庫中提取內容

在大數據時代,數據是一種非常寶貴的資源。許多網站都提供API接口,供開發(fā)人員使用,但對于一些沒有API接口的網站,我們需要使用其他方法來提取數據。本文將介紹如何使用C語言編程從網頁數據庫中提取內容。

1. 網頁抓取

我們需要進行網頁抓取。在C語言中,我們可以使用socket和HTTP協議來實現網頁抓取。以下是一個簡單的示例,使用socket連接目標網站,并獲取網頁內容:

“`c

#include

#include

#include // 在 Windows 平臺需要添加該頭文件

#pragma comment(lib,”ws2_32.lib”) // Windows 平臺需要添加這行代碼

int mn()

{

WSADATA wsaData;

WSAStartup(MAKEWORD(2,1), &wsaData); // 初始化WinSock庫

SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // 創(chuàng)建套接字

SOCKADDR_IN serverAddr;

serverAddr.sin_family = AF_INET;

serverAddr.sin_port = htons(80);

serverAddr.sin_addr.s_addr = inet_addr(“192.168.0.1”);

connect(s, (SOCKADDR*)&serverAddr, sizeof(serverAddr)); // 連接到目標服務器

char buf[4096];

char request[1024];

sprintf_s(request, “GET / HTTP/1.1\r\nHost: 192.168.0.1\r\nConnection: close\r\n\r\n”); // 發(fā)送 GET 請求

send(s, request, strlen(request), 0); // 發(fā)送請求

int nBytes;

while ((nBytes = recv(s, buf, sizeof(buf), 0)) > 0) // 接收響應

{

buf[nBytes] = 0; // 添加字符串結束符

printf(“%s”, buf); // 輸出響應

}

closesocket(s); // 關閉套接字

WSACleanup(); // 釋放 WinSock 庫資源

return 0;

}

“`

2. 解析HTML

接下來,我們需要解析HTML代碼,找到我們需要的數據。我們可以使用libxml2庫來解析HTML,以下是一個簡單的示例:

“`c

#include

#include

void startElement(void *ctx, const xmlChar *name, const xmlChar **attrs)

{

printf(“start element: %s\n”, name);

}

void endElement(void *ctx, const xmlChar *name)

{

printf(“end element: %s\n”, name);

}

int mn()

{

htmlSAXHandler saxHandler = {0};

saxHandler.startElement = startElement;

saxHandler.endElement = endElement;

xmlSAXUserParseMemory(&saxHandler, NULL, “

hello world

“, strlen(“

hello world

“));

return 0;

}

“`

上述代碼解析了一個簡單的HTML代碼,并打印了標簽的起始和結束。

3. 解析ON

除了解析HTML,我們還需要解析ON格式的數據。對于ON格式的數據,我們可以使用cON庫來解析。以下是一個示例:

“`c

#include

#include

#include “cON.h”

int mn()

{

char jsonStr[] = “{\”name\”:\”John\”,\”age\”:30,\”city\”:\”New York\”}”;

cON *json = cON_Parse(jsonStr);

if (json == NULL)

{

printf(“parse error\n”);

return -1;

}

cON *name = cON_GetObjectItem(json, “name”);

if (name == NULL)

{

printf(“name not found\n”);

cON_Delete(json);

return -1;

}

printf(“name: %s\n”, name->valuestring);

cON *age = cON_GetObjectItem(json, “age”);

if (age == NULL)

{

printf(“age not found\n”);

cON_Delete(json);

return -1;

}

printf(“age: %d\n”, age->valueint);

cON *city = cON_GetObjectItem(json, “city”);

if (city == NULL)

{

printf(“city not found\n”);

cON_Delete(json);

return -1;

}

printf(“city: %s\n”, city->valuestring);

cON_Delete(json);

return 0;

}

“`

上述代碼解析了一個簡單的ON,并輸出了其中的每個字段。

4. 數據庫操作

我們需要將抓取到的數據存儲到數據庫中。對于SQLite數據庫,我們可以使用SQLite3庫來實現。以下是一個簡單的示例:

“`c

#include

#include

#include

int mn()

{

sqlite3 *db;

int rc = sqlite3_open(“test.db”, &db);

if (rc != SQLITE_OK)

{

printf(“open database error\n”);

return -1;

}

char *sql = “CREATE TABLE IF NOT EXISTS person (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL);”;

rc = sqlite3_exec(db, sql, NULL, NULL, NULL);

if (rc != SQLITE_OK)

{

printf(“execute error\n”);

return -1;

}

sql = “INSERT INTO person (name, age) VALUES (‘John’, 30);”;

rc = sqlite3_exec(db, sql, NULL, NULL, NULL);

if (rc != SQLITE_OK)

{

printf(“execute error\n”);

return -1;

}

sqlite3_close(db);

return 0;

}

“`

上述代碼創(chuàng)建了一個名為test.db的SQLite數據庫,并創(chuàng)建了一個person表。然后插入了一個John的記錄。

成都網站建設公司-創(chuàng)新互聯,建站經驗豐富以策略為先導10多年以來專注數字化網站建設,提供企業(yè)網站建設,高端網站設計,響應式網站制作,設計師量身打造品牌風格,熱線:028-86922220

高分求c#網頁數據保存:保存網頁內容到數據庫中 代碼

///

/// 讀取指定URL地址,存到指定文件中

///

public bool GetSource(Encoding PageEncoding)

{

bool isGet = false;

try

{

WebRequest request = WebRequest.Create(this.url);

//迅嘩使用代理服務器的處理

if (this.proxyState == 1)

{

//畝陸行默認讀取80端口的數據

if (this.proxyPort == null)

this.ProxyPort = “80”;

WebProxy myProxy = new WebProxy();

myProxy = (WebProxy)request.Proxy;

myProxy.Address = new Uri(this.ProxyAddress + “:” + this.ProxyPort);

myProxy.Credentials = new NetworkCredential(this.proxyAccount, this.proxyPassword, this.ProxyDomain);

request.Proxy = myProxy;

}

//請求服務

WebResponse response = request.GetResponse();

//返回信息

Stream resStream = response.GetResponseStream();

StreamReader sr = new StreamReader(resStream, PageEncoding);

string tempCode = sr.ReadToEnd();

resStream.Close();

sr.Close();

this.outString = tempCode;

//如悉盯果輸出文件路徑為空,便將得到的內容賦給OutString屬性

if (this.outFilePath != null)

{

FileInfo fi = new FileInfo(this.outFilePath);

//如果存在文件則先干掉

if (fi.Exists)

fi.Delete();

StreamWriter sw = new StreamWriter(this.outFilePath, true, Encoding.Default);

sw.Write(tempCode);

sw.Flush();

sw.Close();

}

return !isGet;

}

catch

{

this.noteMessage = “出錯了,請檢查網絡是否連通;”;

return isGet;

}

}

以上是把網頁的內容取了回來,接下來,你把那個OutString存到你的數據庫就行了.

這個保存數據到數據坦塌磨庫這里那你會不會了,會的話就請看下面,不衫銷會的話那就去學下這個ADO.NET這個最基本的知識點.

protected string getHtml(string url)

{

HttpWebRequest webRequest = null;

HttpWebResponse webResponse = null;

StreamReader streamReader = null;

string content=string.Empty;

try

{

webRequest = (HttpWebRequest)WebRequest.Create(url);

webRequest.Method = “GET”;

webResponse = (HttpWebResponse)webRequest.GetResponse();

Stream stream = webResponse.GetResponseStream();

streamReader = new StreamReader(stream, System.Text.Encoding.GetEncoding(“GB2312”));

content = streamReader.ReadToEnd();

}

catch

{

throw;

}

finally

{

streamReader.Close();

webResponse.Close();

}

return content;

}

protected void Button1_Click(object sender, EventArgs e)

{

string url = this.TextBox1.Text.Trim();

Response.Write(getHtml(url));

}

記讓斗得導入命名空間System.Net;System.IO;

這樣通過按鈕操作就可以讀取某個網址的的HTML代碼。返回的是一個string字符串類型!

然后就可以保存到數據庫了!

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

//增加以下命名空蔽昌間

using System.Net;

using System.IO;

using System.Data.OleDb;

namespace PageCodeDownloader

{

public partial class MainForm : Form

{

private string dbConnectionString = string.Empty;

private OleDbConnection dbConnection = null;

private OleDbCommand dbCommand = null;

public MainForm()

{

InitializeComponent();

//界面上增加一個TextBox,修改Name屬性為:txtPageUrl

//界面上增加一個Button,修改Name屬性為btnDownload

//新建一數據庫名為db.mdb

//增加一張大消表:download

//表字段: id 自動編號

//url 文本(255)

//宏仿扒pagecode 備注

this.dbConnectionString = @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\db.mdb”;

this.dbConnection = new OleDbConnection(this.dbConnectionString);

}

private void btnDownload_Click(object sender, EventArgs e)

{

if (txtPageUrl.Text != “”)

{

try

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtPageUrl.Text);

Stream responseStream = request.GetResponse().GetResponseStream();

StreamReader sr = new StreamReader(responseStream,Encoding.Default);

string strPageCode = sr.ReadToEnd();

sr.Close();

string sql = string.Format(“Insert into (,) values(‘{0}’,@PageCode)”,txtPageUrl.Text);

this.dbCommand = new OleDbCommand(sql, this.dbConnection);

this.dbCommand.CommandType = CommandType.Text;

if (this.dbCommand.Connection.State == ConnectionState.Closed)

{

this.dbCommand.Connection.Open();

}

this.dbCommand.Parameters.Add(new OleDbParameter(“@PageCode”,strPageCode));

int EffectRowCount = this.dbCommand.ExecuteNonQuery();

if (EffectRowCount > 0)

{

MessageBox.Show(“保存成功”);

}

else

{

MessageBox.Show(“保存失敗”);

}

}

catch (Exception err)

{

MessageBox.Show(err.Message);

}

}

else

{

MessageBox.Show(“請輸入要獲取代碼的Url”);

}

}

}

}

private void btnSubmit_Click ( object sender, EventArgs e )

{

string strURL = this.txtURl.Text.Trim ( );

Regex regex = new Regex ( @”http(s)?://(+\.)++(/*)?”, RegexOptions.IgnoreCase );

Match match = regex.Match ( strURL );

if ( match.Success )

{

WebClient client = new WebClient ();

try

{

string strResult = client.DownloadString ( strURL );

this.SaveResult ( strResult );

}

catch ( Exception ex )

{

MessageBox.Show ( ex.Message );

}

finally

{

client.Dispose ( );

}

}

else

{

MessageBox.Show ( strURL + “不是一個有效的網址, 請重新輸入!”, “信息提示”, MessageBoxButtons.OK );

this.txtURl.Select ( );

}

}

private void SaveResult ( string result )

{

OleDbConnection connection = new OleDbConnection ( “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Data\\Database.mdb” );

try

{

connection.Open ( );

OleDbCommand command = connection.CreateCommand ( );

command.CommandType = CommandType.Text;

command.CommandText = “INSERT INTO () VALUES(@Content)”;

OleDbParameter param = new OleDbParameter();

param.ParameterName = “@Content”;

param.Value = result;

command.Parameters.Add(param);

int RowCount = command.ExecuteNonQuery ( );

if ( RowCount > 0 )

{

MessageBox.Show ( “網頁抓取并保存成功.” );

}

else

{

MessageBox.Show ( “網頁成功,但是保存失敗.” );

}

}

catch ( OleDbException ex )

{

MessageBox.Show ( ex.Message );

}

finally

{

connection.Close ( );

}

}

代碼沒空寫,方法可以提供:

1.界面最簡單,PASS

2.連接數據庫語句,連接SQL或ACCESS需要確定用何種方式,直接連還是ODBC。

3。設計數據庫結構,如何存儲你的網頁,其實是網頁信褲睜息摘姿鋒取了。

4.連接數據庫過程:打開數據庫,建立連接,傳遞查詢語句,接收返回值,由返回值判斷結跡純晌果返回給界面。

要想學習,別再要代碼,切記。

C++用libcurl庫GET網頁(比如baidu.com)并將獲取到的網頁內容保存到本地文件夾中

#include

#include “curl/curl.h”

#pragma comment(lib, “ws2_32.lib”)

#pragma comment ( lib, “l(fā)ibcurl.lib” )

#pragma comment ( lib, “ws2_32.lib” )

#pragma comment ( lib, “winmm.lib” )

#pragma comment ( lib, “wldap32.lib” )

//這是libcurl接收數據的回調函數,相當于recv的死循環(huán)

//其中stream可以自定義數據類型,這里我傳入的是文件保存路徑

static size_t write_callback( void *ptr, size_t size, size_t nmemb, void *stream )

{

int len = size * nmemb;

int written = len;

FILE *fp = NULL;

if ( access( (char*)stream, 0 ) == -1 )

{

fp = fopen( (char*) stream, “wb” );

}

else

{

fp = fopen( (char*) stream, “ab” );

}

if (fp)

{

fwrite( ptr, size, nmemb, fp );

}

return written;

}

int GetUrl( const char *url, char *savepath )

{

CURL *curl;

CURLcode res;

struct curl_slist *chunk = NULL;

curl = curl_easy_init();

if ( curl ) {

curl_easy_setopt( curl, CURLOPT_VERBOSE, 0L );

curl_easy_setopt( curl, CURLOPT_URL, url );

//指定回調函數

curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_callback);

//這個變量可作為接收或傳遞數據的作用

curl_easy_setopt( curl, CURLOPT_WRITEDATA, savepath );

res = curl_easy_perform( curl );

if (res == CURLE_OK)

{

return 1;

}

return 0;

}

}

int main( void )

{

if ( GetUrl( “t.sin.cn”, “c:/1.txt” ) )

{

printf( “OK” );

}

return 0;

}

如何用一個C#頁面查詢數據庫內數據并在頁面中顯示查詢的內容

因為無法確定你使用的具體技術,所以沒法告訴你具體操作,但是渣族大致用到的技術如下:

html(寫網頁)

數據庫(查詢數據)

一門服務端語言(含配連接html和數據庫)

服務器部談梁指署(項目發(fā)布)

域名

c 編程提取網頁數據庫中內容的介紹就聊到這里吧,感謝你花時間閱讀本站內容,更多關于c 編程提取網頁數據庫中內容,用C編程從網頁數據庫中提取內容,高分求c#網頁數據保存:保存網頁內容到數據庫中 代碼,C++用libcurl庫GET網頁(比如baidu.com)并將獲取到的網頁內容保存到本地文件夾中,如何用一個C#頁面查詢數據庫內數據并在頁面中顯示查詢的內容的信息別忘了在本站進行查找喔。

創(chuàng)新互聯是成都專業(yè)網站建設、網站制作、網頁設計、SEO優(yōu)化、手機網站、小程序開發(fā)、APP開發(fā)公司等,多年經驗沉淀,立志成為成都網站建設第一品牌!


當前文章:用C編程從網頁數據庫中提取內容(c編程提取網頁數據庫中內容)
本文路徑:http://m.5511xx.com/article/dpespeo.html