2016-11-22 3 views
0

this siteからの使用例文を取ります。ここで辞書Web-Scraperは例文を返しません

は、ページのHTMLソースです:

<vcom:examples lang="en" word="creep" count="4" filter="0" class="vcom_examples"> 

<div class="exampleBrowser hasNext"> 

<div class="domains"> 
<a href="javascript:void(0)" title="All Sources" class="selected">All Sources</a><a href="javascript:void(0)" title="Fiction" data-code="F">Fiction</a><a href="javascript:void(0)" title="Arts/Culture" data-code="A">Arts/Culture</a><a href="javascript:void(0)" title="News" data-code="N">News</a><a href="javascript:void(0)" title="Business" data-code="B">Business</a><a href="javascript:void(0)" title="Sports" data-code="S">Sports</a><a href="javascript:void(0)" title="Science/Med" data-code="M">Science/Med</a><a href="javascript:void(0)" title="Technology" data-code="T">Technology</a></div> 

<div class="container" style="height: auto;"> 
<div class="results" style="left: 0px;"> 

<ul> 
<li><div class="sentence"> 
If you believe their campaigns, it’s the choice between a <strong>creep</strong> and a crook.</div> 
<a target="_blank" class="source" href="https://www.theguardian.com/us-news/2016/nov/22/journalists-media-election-2016-donald-trump"> 
<span class="corpus">The Guardian</span> 
<span class="date">Nov 22, 2016</span></a> 
</li> 

<li> 
<div class="sentence"> 
From stingrays to spy planes, we are seeing the consequences of powerful surveillance technology <strong>creeping</strong> into local law enforcement without adequate limits. 
</div> 
<a target="_blank" class="source" href="http://www.slate.com/articles/technology/future_tense/2016/11/should_police_bodycams_come_with_facial_recognition_software.html"> 
<span class="corpus">Slate</span> 
<span class="date">Nov 22, 2016</span></a> 
</li> 

</ul></div></div> 

<div class="buttons"><a class="prev ss-navigateleft" title="prev">Prev</a><a class="next ss-navigateright right" title="next">Next</a></div></div></vcom:examples> 

、ここでは私のPythonコードです:

import bs4 as bs 
import urllib.request 

sauce = urllib.request.urlopen('https://www.vocabulary.com/dictionary/creep').read() 
soup = bs.BeautifulSoup(sauce,'lxml') 

for examples in soup.find_all('p',class_ = 'sentence'): 
    print(examples.text) 

それは上記と同様に単語の意味をこすることに成功しました。 しかし、そのような例文を掻き集めると、何も返されませんでした。

例文が返されないのはなぜですか?

+1

センテンスは 'div'タグの中にあります。なぜあなたは' soup.find_all( 'p'、class_ = 'sentence') 'を持っていますか? – mx0

+0

oh-間違ったコードをコピーしました。私のミス。 私はdivでそれをやった。それは動作しません。 –

答えて

0

例文は後でjsonからロードされるので、beautifulsoup4でその文を得ることはできません。 HTMLレスポンスでは、このセクションは次のようになります。

<vcom:examples lang="en" word="creep" count="4" filter="0" ></vcom:examples> 

https://cdn.vocab.com/js/module-esekdz.jsのコードで後で入力します。

の代わりにあなただけの言葉、ドメインとmaxの結果を選択し、JSONから直接例文を取得するには、この最小限のスニペットを使用することができます全体のhtml体の解析:urllib.request.urlopenでの試験の多くの後

import requests 

search_word = 'creep' 
# "Fiction", "Arts/Culture", "News", "Business", "Sports", "Science/Med", "Technology" 
domains = [None, "F", "A", "N", "B", "S", "M", "T"] 

link = "https://corpus.vocabulary.com/api/1.0/examples.json" 

response = requests.get(link, params={'query': search_word, 'domain': domains[0], 'maxResults': 24}) 

if response.ok: 
    for example in response.json()['result']['sentences']: 
     print(example['sentence']) 
0

を(またはrequests.get)とbs4と、私はまた、sentenceクラスを取得することに失敗しました。 Webページから見ることができるように、どちらのメソッドもコンテンツ全体を取得できないようです。この場合、代わりにselenium.webdriverなどの他のパッケージを使用する必要があります。次のようなコードは次のようになります。ここでは

from selenium import webdriver 

chrome_driver_path = 'your_working_directory\\chromedriver.exe' 
mydriver = webdriver.Chrome(chrome_driver_path) 

mydriver.get('https://www.vocabulary.com/dictionary/creep') 
sentences = mydriver.find_elements_by_class_name('sentence') 

for sentence in sentences: 
    print(sentence.text) 

は、Webスクレイピングを行うために「セレン/ ChromeDriver」の使用方法については、非常に明確かつ簡潔である、video tutorialです。

関連する問題