2011-12-18 51 views
0

我想讓文本文件包含很多阿拉伯文字,所以我想用python打開網站:urlopen函數並將這個單詞保存到列表中然後將它導出到文本文件。 我在新的Python任何幫助我都會感激從網站寫文本列表到txt文件

+2

你只是想阿拉伯語詞彙列表?如何阿拉伯語字典? – 2011-12-18 22:05:52

+0

我不知道你在問什麼。什麼是文本文件和網站的東西應該是什麼意思?有沒有一個網站的話? – birryree 2011-12-18 22:09:23

+0

聽起來像'urllib'和're'的工作。你試過什麼了? (碼) – hochl 2011-12-18 22:21:26

回答

1

從網絡上保存文件:

import urllib2 

u = urllib2.urlopen('http://www.your-url-here.com/filename.txt') 
f = open('myfile.txt', 'w') 
f.write(u.read()) 
f.close() 
0

執行以下操作:

  1. 從包含該網站提取HTML文字
  2. 清除html標記和符號
  3. 提取單詞。
  4. 濾除噪聲

爲可以使用NLTK 2-次和3次點。 下面是一個例子如何可以實現:

import nltk 
import urllib2 
u = urllib2.urlopen('http://www.google.com')# replace google with your arabic site of interest 
UnwantedSymbols='|&;.,-!'#real words don't contain these symbols, add yours 
html=u.read() 
raw = nltk.clean_html(html) 
tokens = nltk.word_tokenize(raw) 
filename='arabicwords.txt' 
f=open(filename,'w') 
for token in tokens: 
    write=True 
    for symbol in UnwantedSymbols: 
     if symbol in token: 
      write=False 
      break 
    if write: 
     f.write(token+'\n')# if no unwanted symbol was encountered, then write the word to the file 
f.close()