如何获取本地html文件的标题,html超级链接代码

查看: 1094|回复: 8
如何批量抓取一个文件夹下所有html文件的标题
阅读权限10
在线时间 小时
本帖最后由 hzjsya 于
09:04 编辑
如何批量抓取一个文件夹下所有html文件的标题
(即&title& &/title& 标签之间的内容)
&title&Excel教程学习,Excel表格交流,Excel技巧培训教程下载-ExcelHome技术论坛 - &/title&
文件名标题example.htmlExcel教程学习,Excel表格交流,Excel技巧培训教程下载-ExcelHome技术论坛 -
阅读权限10
在线时间 小时
如何实现?
阅读权限20
在线时间 小时
使用FileSystem对象和正则表达式对象regex
阅读权限20
在线时间 小时
本帖最后由 河坊古道 于
18:54 编辑
试了一下,你看看行不行
Sub 提取标题()
& & Dim i%, j%, Var$, arr() As String
& & Path = ThisWorkbook.Path & &&
& & f = Dir(Path & &*.html&)
& & Application.ScreenUpdating = False
& &&&Range(&a2:c& & Range(&b65536&).End(xlToRight).Row) = &&
& &&&Do While f && &&
& && &&&Open Path & f For Input As #1
& && &&&s = Split(Replace(StrConv(InputB(LOF(1), 1), vbUnicode), Chr(9), &&), &&&)
& && &&&Close #1
& && &&&For i = 0 To UBound(s)
& && && && &If InStr(s(i), &&/title&) Or InStr(s(i), &&/TITLE&) Then
& && && && && & Var = && & Split(Split(s(i), &&&)(0), &&)(0)
& && && && && & j = j + 1
& && && && && & ReDim Preserve arr(1 To 3, 1 To j)
& && && && && &arr(1, j) = &文件名& & j
& && && && && &arr(2, j) = Var
& && && && && &arr(3, j) = Split(f, &.&)(0)
& && && && && &&&Cells(j + 1, 3).Select
& && && && && &ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=Path & f, TextToDisplay:=arr(3, j)
& && && && && &&&Exit For
& && && && &End If
& && &&&Next
& && &&&f = Dir()
& & Loop
& & Sheet1.[a2].Resize(UBound(arr, 2), 3) = WorksheetFunction.Transpose(arr)
& && &Application.ScreenUpdating = True
End Sub复制代码
(19.22 KB, 下载次数: 2)
18:54 上传
18:48 上传
点击文件名下载附件
56.13 KB, 下载次数: 41
阅读权限90
在线时间 小时
建议你上传附件并模拟出效果!
阅读权限10
在线时间 小时
试了一下,你看看行不行
可以,多谢了
阅读权限10
在线时间 小时
本帖最后由 hzjsya 于
18:22 编辑
试了一下,你看看行不行& && & Open Path & f For Input As #1
& && &&&s = Split(Replace(StrConv(InputB(LOF(1), 1), vbUnicode), Chr(9), &&), &&&)
& && &&&Close #1
& && &&&For i = 0 To UBound(s)
& && && && &If InStr(s(i), &]&) Or InStr(s(i), &&/TITLE&) Then
& && && && && & Var = && & Split(Split(s(i), &&&)(0), &&)(0)
请问这几句是什么意思
阅读权限50
在线时间 小时
多网上下载网页,再从网页导入标题,是不是多了一个环节呢,不如直接网抓网页内容。
阅读权限10
在线时间 小时
多网上下载网页,再从网页导入标题,是不是多了一个环节呢,不如直接网抓网页内容。
网页需要cookies认证并且比较多
最新热点 /1
ExcelHome每周都有线上直播公开课,
国内一流讲师真身分享,高手贴身答疑,
赶不上直播还能看录像,
关键居然是免费的!
厚木哥们都已经这么努力了,
你还好意思说学不好Office。
玩命加载中,请稍候
玩命加载中,请稍候
Powered by
本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任! & & 本站特聘法律顾问:徐怀玉律师 李志群律师java读取超链接的title和内容_cute董彩凤_新浪博客
java读取超链接的title和内容
package com.baiyu.
import java.io.BufferedR &
import java.io.IOE &
import java.io.InputStreamR &
import java.net.MalformedURLE
import java.net.URL; &
import java.util.ArrayL &
import java.util.HashM &
import java.util.L &
import java.util.regex.M &
import java.util.regex.P &
public class WebContent { &
&public String getOneHtml(final String htmlurl)
throws IOException &
& final StringBuffer sb = new StringBuffer();
& &url = new URL(htmlurl);
& &final BufferedReader in =
new BufferedReader(new InputStreamReader(url.openStream(),
"utf-8"));// 读取网页全部内容 &
& &while ((temp =
in.readLine()) != null) &
& & sb.append(temp);
& &in.close();
& catch (final MalformedURLException me)
&System.out.println("你输入的URL格式有问题!请仔细输入");
& &me.getMessage();
& catch (final IOException e)
& &e.printStackTrace();
& return sb.toString(); &
&public String getTitle(final String s)
& String title = ""; &
& final List list = new ArrayList();
& regex = "& t
itle&.*?& / t itle&";
& final Pattern pa = pile(regex,
Pattern.CANON_EQ); &
& final Matcher ma = pa.matcher(s);
& while (ma.find()) &
& &list.add(ma.group());
& for (int i = 0; i &
list.size(); i++) &
& &title = title +
list.get(i); &
& return outTag(title); &
&public List getLink(final String s)
& final List list = new ArrayList();
& regex = "&
a[^&]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s&]*))[^&]*&(.*?)&
/ &a &"; &
& final Pattern pa = pile(regex,
Pattern.DOTALL); &
& final Matcher ma = pa.matcher(s);
& while (ma.find()) &
& &list.add(ma.group());
&public List getScript(final String s)
& final List list = new ArrayList();
& regex = "&
script.*?& / s cript &";
& final Pattern pa = pile(regex,
Pattern.DOTALL); &
& final Matcher ma = pa.matcher(s);
& while (ma.find()) &
& &list.add(ma.group());
&public List getCSS(final String s)
& final List list = new ArrayList();
& regex = "& st
yle.*?& / styl e &";
& final Pattern pa = pile(regex,
Pattern.DOTALL); &
& final Matcher ma = pa.matcher(s);
& while (ma.find()) &
& &list.add(ma.group());
&public String outTag(final String s)
s.replaceAll("&.*?&", "");
&public HashMap getFromYahoo(final String s)
& final HashMap hm = new HashMap();
& final StringBuffer sb = new StringBuffer();
& String html = ""; &
System.out.println("\n------------------开始读取网页(" + s +
")--------------------"); &
& &html = getOneHtml(s);
& catch (final Exception e)
& &e.getMessage();
& // System.out.println(html);
& System.out.println("------------------读取网页("
+ s + ")结束--------------------\n"); &
& System.out.println("------------------分析(" +
s + ")结果如下--------------------\n"); &
& String title = outTag(getTitle(html));
& title = title.replaceAll("_雅虎知识堂", "");
& // Pattern pile("
class=\"original\"&(.*?)((\r\n)*)(.*?)((\r\n)*)(.*?)
",Pattern.DOTALL); &
& final Pattern pa =
pile("& &div
class=\"original\"&(.*?)& /
/div&", Pattern.DOTALL); &
& final Matcher ma = pa.matcher(html);
& while (ma.find()) &
& &sb.append(ma.group());
& String temp = sb.toString();
& temp = temp.replaceAll("(& b r
&)+?", "\n");// 转化换行 &
& temp = temp.replaceAll("& p
&/ &p&", "");//
去图片注释 &
& hm.put("title", title);
& hm.put("original", outTag(temp));
&public static void main(final String args[])
& String url = ""; &
& final List list = new ArrayList();
& System.out.print("输入URL,一行一个,输入结束后输入 go
程序开始运行: & \n"); &
& final BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
& &while (!(url =
br.readLine()).equals("go")) &
& & list.add(url);
& catch (final Exception e)
& &e.getMessage();
& final WebContent wc = new WebContent();
& HashMap hm = new HashMap();
& for (int i = 0; i &
list.size(); i++) &
wc.getFromYahoo(list.get(i)); &
& &System.out.println("标题: " +
hm.get("title")); &
& &System.out.println("内容: \n"
+ hm.get("original")); &
& // System.out.println(htmlurl+"网页内容结束");
cute董彩凤
博客等级:
博客积分:0
博客访问:34,148
关注人气:0
荣誉徽章:本文用于提取本地网页的标签元素如&TITLE&&/TITLE&,&IMG&,&A&&/A&...的内容,非常实用于批量文件的操作,这是按一般文件进行文本查找替换无法比拟的,,而这是使用TWEBBROWSER控件无法做到的。类似的,你可以把本地的HTML文件转换成MHT文件(这是个大家觉得很棘手的问题,本人已经搞定)。
//uses activex,mshtml
function Html_GetTitleFromFile(const HtmlFile:TFileNvar FileTitle:String):BvarIdoc&&&& : IHtmlDocument2;//ElementGroup : IhtmlElementC//HtmlItem: IHTMLEPersistFile: IPersistFbeginResult:=Fif not fileexists(HtmlFile) then& FileTitle:=;
& try&&&&&& Idoc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;&&&&&& PersistFile := IDoc as IPersistF&&&&&& if PersistFile.Load(StringToOleStr(HTMLFile),1)&&S_OK then&&&&&&&&&&&& IDoc.designMode :=& //This will disable script execution.&&& {&& while IDoc.readyState && complete do& //if it dead here,how to do it?&&&&&& begin&&&&&& application.ProcessM&&&&&&&&& }&&& //&& Showmessage(IDoc.readyState);&&&&&& Application.ProcessM&&&&&& sleep(1000);&&& //&& Showmessage(IDoc.readyState);&&&&&& if IDoc.readyState&&complete then&&&&&& begin&&&&&& Application.ProcessM&&&&&& sleep(1000);&&&&&&&&&&&& if IDoc.readyState&&complete then&&&&&& begin&&&&&& IDoc:=&&&&&& Result:=F&&&&&&&&&&&&&&&&&&& Result:=T&&&&&& FileTitle:=IDoc.&&&&&& {& //This code also works&&&&&& ElementGroup:=IDoc.all.tags(TITLE) As IhtmlElementC&&&&&& HtmlItem:=ElementGroup.item(0,0) As IHtmlE&&&&&& FileTitle:=HtmlItem.innerT&&&&&& }
&& finally&&&&&&& IDoc :=&&
文章整理:西部数码--专业提供、服务以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
Views(...) Comments()如何获取本地html文件的标题,超级链接
我的图书馆
如何获取本地html文件的标题,超级链接
本文用于提取本地网页的标签元素如&TITLE&&/TITLE&,&IMG&,&A&&/A&...的内容,非常实用于批量文件的操作,这是按一般文件进行文本查找替换无法比拟的,,而这是使用TWEBBROWSER控件无法做到的。类似的,你可以把本地的HTML文件转换成MHT文件(这是个大家觉得很棘手的问题,本人已经搞定)。
//uses activex,mshtml
function Html_GetTitleFromFile(const HtmlFile:TFileNvar FileTitle:String):BvarIdoc&&&& : IHtmlDocument2;//ElementGroup : IhtmlElementC//HtmlItem: IHTMLEPersistFile: IPersistFbeginResult:=Fif not fileexists(HtmlFile) then& FileTitle:=;
& try&&&&&& Idoc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;&&&&&& PersistFile := IDoc as IPersistF&&&&&& if PersistFile.Load(StringToOleStr(HTMLFile),1)&&S_OK then&&&&&&&&&&&& IDoc.designMode :=& //This will disable script execution.&&& {&& while IDoc.readyState && complete do& //if it dead here,how to do it?&&&&&& begin&&&&&& application.ProcessM&&&&&&&&& }&&& //&& Showmessage(IDoc.readyState);&&&&&& Application.ProcessM&&&&&& sleep(1000);&&& //&& Showmessage(IDoc.readyState);&&&&&& if IDoc.readyState&&complete then&&&&&& begin&&&&&& Application.ProcessM&&&&&& sleep(1000);&&&&&&&&&&&& if IDoc.readyState&&complete then&&&&&& begin&&&&&& IDoc:=&&&&&& Result:=F&&&&&&&&&&&&&&&&&&& Result:=T&&&&&& FileTitle:=IDoc.&&&&&& {& //This code also works&&&&&& ElementGroup:=IDoc.all.tags(TITLE) As IhtmlElementC&&&&&& HtmlItem:=ElementGroup.item(0,0) As IHtmlE&&&&&& FileTitle:=HtmlItem.innerT&&&&&& }
&& finally&&&&&&& IDoc :=&&
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&
喜欢该文的人也喜欢

我要回帖

更多关于 html5 超级链接 的文章

 

随机推荐