1

我的任务是从 URL 列表中提取标题和元描述。我用过鹅。我做得对吗?

from goose import Goose import urlparse import numpy as np import os import pandas

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])
4

1 回答 1

0

您可以打开网址并将其分配给任何频道。当您阅读它并存储在任何变量中时,这将是带有 html 标记和值的页面源。您可以使用与您的搜索条件匹配的正则表达式从该页面获取所需的信息。你可以这样做:

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

可变页面将为您提供所有 html 页面标签和结构。您可以编写任何常规 expersion 来获取所需的详细信息。说 re.findall(r'https?://.*?/', page),会给你所有的网址。同样,您可以从页面获取所需的详细信息

于 2016-06-23T07:48:39.987 回答