2016-06-23 4 views
1

URLのリストからtitleとmeta_descriptionを抽出するタスクが与えられました。私はガチョウを使いました。私はそれを正しくしていますか?ガチョウの輸入グースからpythonを使用してURLから抽出したタイトルを保存するにはどうすればよいですか?

輸入NP 輸入OSとしてインポートnumpyの urlparse 輸入パンダ

os.chdir("C:\Users\EDAWES01\Desktop\Cookie profiling") 
data = pandas.read_csv('activity_url.csv', delimiter=';') 
data_read=np.array(data) 
quantity = data_read[0:, 2] 
url_data = data_read[quantity==1][0:3,1] 
user_id = data_read[quantity==1][0:3,0] 
url_data 

#remove '~oref=' 
clean_url_data=[] #intialize 
for i in xrange(0,len(url_data)): 
    clean_url_data.append(i) 
    clean_url_data[i]=urlparse.urlparse(url_data[i])[2].split("=") 
    clean_url_data[i]=clean_url_data[i][1] 

clean_url_data=np.array([clean_url_data]) 

#store title 
website_title=[] 
#store meta_description 
website_meta_description=[] 


g=Goose() 

for urlt in xrange(0, len(clean_url_data)): 
    website_title.append(urlt) 
    website_title[urlt]=g.extract(clean_url_data[urlt]) 
    website_title[urlt]=website_title[urlt].title 

website_title=np.array([website_title]) 

for urlw in xrange(0, len(clean_url_data)): 
    website_meta_description.append(urlw) 
    website_meta_description[urlw]=g.extract(clean_url_data[urlw]) 
    website_meta_description[urlw]=website_meta_description[urlw].meta_description 


website_meta_desciption=np.array([website_meta_description]) 

答えて

0

URLを開き、任意のチャンネルに割り当てることができます。それを読んで任意の変数に格納すると、htmlタグと値を持つページソースになります。そのページから必要な情報は、検索条件に一致する正規表現を使用して取得できます。あなたはこのような何かを行うことができます。

sock = urllib2.urlopen('http://www.google.co.in') 
page = sock.read() 
sock.close() 
listOfUrls = re.findall(r'https?://.*?/', page) 

変数ページでは、すべてのHTMLページのタグと構造を与えるだろう。 必要な詳細を取得するために、任意の定期的な展開を記述することができます。 re.findall(r'https?://.*?/ '、page)と言って、あなたにすべてのURLを渡します。 同様に、ページから必要な情報を取得できます

関連する問題