日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自然语言处理----处理原始文本

發布時間:2024/4/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自然语言处理----处理原始文本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要介紹編程訪問網絡文本的幾種方式。

1. 訪問網絡資源

>>> from urllib import urlopen >>> url='http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html' >>> raw=urlopen(url).read() >>> type(raw) <type 'str'> >>> len(raw) 16429 >>> raw[:75] '\n\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n "http://' View Code

如果Python無法正確自動檢測出Internet代理,可以使用下面方法手動指定。

>>> proxies={'http': 'http://www.someproxy.com:3128'} >>> raw=urlopen(url, proxies=proxies).read()

2. 訪問博客

在Universal Feed Parser的第三方python庫的幫助下,可以訪問博客的內容。

>>> import feedparser >>> llog=feedparser.parse('http://weibo.com/ttarticle/p/show?id=2309404116343489194022') >>> llog.keys() ['feed', 'status', 'version', 'encoding', 'bozo', 'headers', 'href', 'namespaces', 'entries', 'bozo_exception'] >>> type(llog['feed']) <class 'feedparser.FeedParserDict'> >>> llog['feed'].keys() ['meta', 'summary'] >>> llog['feed']['meta'] {'content': u'text/html; charset=gb2312', 'http-equiv': u'Content-type'} >>> llog['feed']['summary'] u'<span id="message"></span>\n\n&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;' View Code

3. 處理html

?一般有三種方式:正則匹配, nltk.clean_html(), BeautifulSoup. 正則表達式比較繁瑣,而nltk.clean_html()現在已經不支持了,比較簡單常用的是用BeautifulSoup包。

from bs4 import BeautifulSouphtml_doc=''' <html><head><title>The Document's story</title></head><html><head><title>The Dormouse's story</title></head><body><p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;and they lived at the bottom of a well.</p><p class="story">...</p></body></html> ''' soup = BeautifulSoup(html_doc, 'html.parser') content=soup.get_text() print content

運行結果如下:

runfile('D:/my project/e_book/XXMLV-2/4.Python_代碼/test.py', wdir='D:/my project/e_book/XXMLV-2/4.Python_代碼')The Document's story The Dormouse's story The Dormouse's story Once upon a time there were three little sisters; and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well. ...

?

轉載于:https://www.cnblogs.com/no-tears-girl/p/6964600.html

總結

以上是生活随笔為你收集整理的自然语言处理----处理原始文本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。