2017-10-03 19 views
0

私はこのウェブサイトから記事のウェブクローリングを行います。Xpathでウェブページから記事のテキストを抽出する

これは私がこれまでにやっていることです:

# HR Version 
# the entire crawling process 

openfile = open("data/HR.csv", "rb") 
r = csv.reader(openfile) 
HR_data = [] 

for i in r: 
    url = i[0] 
    print url # to know the status of web crawling 
    r = requests.get(url) 
    data = html.fromstring(r.text) 
    #Inspect line with text 
    #//*[@id="article-details"] 
    #<section class="entry-content clearfix" itemprop="articleBody"></section> 
    texts = data.xpath("//*[@id="article-details"]/p/text()") 
    raw = ''.join(str(i.encode("utf-8")) for i in texts) 
    finaldata = raw.replace('\r','').replace('\n','').replace('\r','').replace('\t','')  
    HR_data.append([finaldata]) 

openfile.close() 

問題のコマンドは、以下の

texts = data.xpath("//*[@id="article-details"]/p/text()") 

であり、それがこの特定のウェブページからです。要素のを点検して使用http://hrmagazine.co.uk/article-details/internal-entrepreneurship-can-boost-your-business

Firefoxの場合、「テキスト」は次のセクションに含まれていることがわかりました:

<article id="article-details"> 
#One <h2> element, followed by multiple <p> elements. 
</article> 

記事からのみ段落テキストを抽出するための正しいXPathは何ですか?

答えて

0

あなたはほぼ正しいXPathを書いています。 をh2に置き換えてください。

texts = data.xpath("//*[@id="article-details"]/h2/text()") 
関連する問題