2017-03-31 8 views
0

美しいスープを使ったり、Web Scrapingをやっているのは初めてのことです。私はこれまでにどれくらいの距離を得ているのか十分に満足していますが、私は少しの障害に遭いました。美しいスープを使ったフォーラムのスクラップ - 引用された返信を除外する方法は?

私は特定のスレッドのすべての投稿を削り取ろうとしています。しかし、私は引用された返信からテキストを除外したい。

An example:

私は赤いボックスで示された領域内のテキストをこすることなく、これらの投稿からテキストをこすりたいと思います。

htmlでは、除外したい部分は、私が難しかったメッセージのために選択する必要があるセクションの中にあります。うまくいけば、これはあなたが私が何を言っているか理解するのに役立ちます:私は、私は以下の私のコードが含まれているHTML

HTML image

<div id="post_message_39096267"><!-- google_ad_section_start --><div style="margin:20px; margin-top:5px; "> 
<div class="smallfont" style="margin-bottom:2px">Quote:</div> 
<table cellpadding="6" cellspacing="0" border="0" width="100%"> 
<tbody><tr> 
    <td class="alt2" style="border:1px inset"> 

      <div> 
       Originally Posted by <strong>SAAN</strong> 
       <a href="http://www.city-data.com/forum/economics/2056372-minimum-wage-vs-liveable-wage-post33645660.html#post33645660" rel="nofollow"><img class="inlineimg li fs-viewpost" src="http://pics3.city-data.com/trn.gif" border="0" alt="View Post" title="View Post"></a> 
      </div> 
      <div style="font-style:italic">I agree with trying to buy a 
cheap car outright, the problem is everyone I know that has done that $2- 
5000 car, always ended up with these huge repair bills that are equivalent 
to car payments. Most cars after 100K will need all sort of regulatr 
maintance that is easily a $200 repair to go along with anything that may 
break which is common with cars as they age.<br> 
<br> 
I have a 2yr old im making payments on and 14yr old car that is paid off, 
but needs $2000 in maintenance. When car shopping this summer, I saw many 
cars i could buy outright, but after adding u everything needed to make sure 
it needs nothing, your back into the price range of a car payment.</div> 

    </td> 
</tr> 
</tbody></table> 
</div>Depends on how long the car loan would be stretched. Just because you 
can get an 8 year loan and reduce payments to a level like the repairs on 
your old car doesn't make it a good idea, especially for new cars that <a 
href="/knowledge/Depreciation.html" title="View 'depreciate' definition from 
Wikipedia" class="knldlink" rel="nofollow">depreciate</a> quickly. You'd 
just be putting yourself into negative equity territory.<!-- 
google_ad_section_end --></div> 

のスクリーンショットが含まれています。あなたは私の仕事と私は文字通り9歳であるかのように私には何の事を説明するだろう何のコード例を与えることができれば

from bs4 import BeautifulSoup 
import urllib2 


num_pages = 101 
page_range = range(1,num_pages+1) 
clean_posts = [] 

for page in page_range: 
    print("Reading page: ", page, "...") 
    if page == 1: 
    page_url = urllib2.urlopen('http://www.city-data.com/forum/economics/2056372-minimum-wage-vs-liveable-wage.html') 
    else: 
    page_url = urllib2.urlopen('http://www.city-data.com/forum/economics/2056372-minimum-wage-vs-liveable-wage'+'-'+str(page)+'.html') 


soup = BeautifulSoup(page_url) 

postData = soup.find_all("div", id=lambda value: value and value.startswith("post_message_")) 

posts = [] 
for post in postData: 
    posts.append(BeautifulSoup(str(post)).get_text().encode("utf-8").strip().replace("\t", "")) 

posts_stripped = [x.replace("\n","") for x in posts] 

clean_posts.append(posts_stripped) 

最後に、私は非常にそれをいただければ幸いです!

乾杯 Diarmaid

答えて

1

チェックあなたのpost_message_のdivを別のdiv内部(引用DIV)を持っている場合。もしそうなら、それを抽出する。元のdiv(post_message_)テキストをリストに追加します。 for post in postDataを交換してください。

posts = [] 
for post in postData: 
    hasQuote = post.find("div") 
    if not hasQuote is None: 
     hasQuote.extract() 
    posts.append(post.get_text(strip=True)) 
+0

はい!ありがとうございました!!!! –

関連する問題