Rss & SiteMap

Foxtable(狐表) http://www.foxtable.com

新一代数据库软件,完美融合Access、Foxpro、Excel、vb.net之优势,人人都能掌握的快速软件开发工具!
共3 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:百度百科——内容如何提取

1楼
lexianfeng 发表于:2018/5/15 21:50:00
 最近想通过webbrowser,来对数据表中的中药名称,进行批量的百度百科内容提取,现在发现webbrowser 不能进行getelementsbyclassname ,而只有通过getelementsbyid 或是 getelementsbytagname 等方法,发现很难做到,不知道有何更好的方法可以采用,本打算自己构建类似方法,发现webbrowser不支持的太多,最终也没能实现,特开贴大家共同研究一下,解决方法。
2楼
lexianfeng 发表于:2018/5/15 22:37:00
 Dim str As String = "https://baike.baidu.com/item/%E5%B7%9D%E8%B4%9D%E6%9E%87%E6%9D%B7%E7%B3%96%E6%B5%86"
Dim classname As String ="main-content"  ' "main-content"

Dim web As New System.Windows.Forms.WebBrowser()
web.Navigate(str )
web.ScriptErrorsSuppressed = True  ''过滤脚本错误

Do Until web.ReadyState = 4  '''等待获取网页数据结束
    Application.DoEvents
Loop

Dim elemts  = web.Document.GetElementsByTagName("body")(0).GetElementsByTagName("div")  '获取body中的div元素
Dim data As String = web.Document.Body.Innerhtml 'Text
'If data.Contains(classname ) Then msgbox("存在" & classname )

'''计算内容页 div的位置
Dim ii,iii As Integer
ii =data.IndexOf(classname )
Dim data2 As String = data.SubString(0,ii)
iii = (data2.Length-data2.Replace("<DIV" ,"").Length)/4
'''输出内容
output.show(elemts(iii-1)  .Innertext)
[此贴子已经被作者于2018/5/15 22:38:54编辑过]
3楼
lexianfeng 发表于:2018/5/15 22:45:00
'''引用 大红袍 的代码,发现更加简单,原来属性的classname 可以用 GetAttribute 获得,收益匪浅

Dim str As String ="https://baike.baidu.com/item/平车前"
'Dim str As String = "https://baike.baidu.com/item/%E5%B9%B3%E8%BD%A6%E5%89%8D"

Dim web As New System.Windows.Forms.WebBrowser()
web.Navigate(str )
web.ScriptErrorsSuppressed = True  ''过滤脚本错误

Do Until web.ReadyState = 4  '''等待获取网页数据结束
    Application.DoEvents
Loop

Dim lis As System.Windows.Forms.HtmlElementCollection = web.Document.GetElementsByTagName("div") '获得当前页面的A标签HTML元素集合
Dim count As Integer = 0
For Each li As object In lis
    If li.GetAttribute("ClassName") = "main-content" Then
msgbox("找到类名为 main-content 的类")
output.show(li.Innertext)
        'count += 1
        'If count = 4 Then
            'li.GetElementsByTagName("a")(0).InvokeMember("Click") '执行该标签的点击方法,实现
        'End If
    End If
Next

共3 条记录, 每页显示 10 条, 页签: [1]

Copyright © 2000 - 2018 foxtable.com Tel: 4000-810-820 粤ICP备11091905号

Powered By Dvbbs Version 8.3.0
Processed in .02100 s, 2 queries.