2017-08-11 16 views
2

私は記事や見出しを集めようとしていますが、分かりにくい部分があります。BeautifulSoupで不要なテキストを解析するには?

url = "http://insideevs.com/" 
page = requests.get(url) 
data = page.text 
soup = BeautifulSoup(data, "lxml") 
latest = [] 
b = soup.find_all('div', class_=re.compile("content")) 
for a in b: 
    latest.append(a.get_text(strip=True)) 

たとえば、リストのすべてのアイテムには、記事に付けられたタイムフレームとコメントがあります。 「2週間前、574件のコメント」など。最後のスニペットを除外する方法をアドバイスできますか?

+0

各項目について、タイトルと略語が必要ですか? –

+0

@BillBellタイトルは素晴らしいでしょう。短縮テキストの意味は? ダイレクトリンクを個別に抽出する方法もありますか? (ごめんなさい、Python初心者です!) – hiimarksman

+0

略語:サムネイル画像の右側に表示される単語は、 '...'で終わっています。はい、あなたは完全な記事へのリンクを得ることもできます。 –

答えて

0

extractを使用して、目的のタグを削除します。あなたのコードから

例:

for a in b: 
    a.find('p', {"class" : "details"}).extract() 
    latest.append(a.get_text(strip=True)) 
0

まず持っている、またはしたいデータ項目に隣接しているh3要素のコレクションを取得するにはBeautifulSoupを使用しています。私は隣り合っていると言います。なぜなら、それらのうちの1つが、それぞれの場合に、h3の兄弟であるからです。

h3項目から、あなたはその中aリンク要素を見つけるために、再びselectメソッドを使用することができますし、そのtextを取得します。必要なテキストはlink要素の兄弟です。しかし、それはいくつかの唯一のものなので、私は:nth-of-type(1)を使って最初のものを要求します。ほとんど忘れてしまった~ pは、「私に兄弟を譲ってください」と言われました。私が呼び出されたものはh3でした。

次に、完全な記事へのリンクは、textの前にリンクのhref属性を尋ねることで得ることができます。

私はこれをすべてenumerateに入れて、出力をページから5つの項目に切り詰めるように整えました。

>>> import requests 
>>> import bs4 
>>> page = requests.get('http://insideevs.com/').content 
>>> soup = bs4.BeautifulSoup(page, 'lxml') 
>>> for i, item in enumerate(soup.select('article div h3')): 
...  title = item.select('a')[0].text 
...  text = item.select('~ p:nth-of-type(1)') 
...  url = item.select('a')[0].attrs['href'] 
...  if i < 5: 
...   title 
...   text[0].text 
...   url 
...   
'Plug-In Volvo XC60 T8 Enters U.S. Next Month With 10.4 kWh Battery' 
'Volvo latest plug-in hybrid, the\xa0premium mid-sized SUV XC60 T8 Twin Engine, debuted in March at the Geneva Motor Show. The car is based on the company’s\xa0SPA vehicle architecture, first used in the 90 series (XC90 and S90). Production of the XC60 actually began in mid-April at the Torslanda Plant in…' 
'http://insideevs.com/plug-in-volvo-xc60-t8-enters-u-s-next-month-with-10-4-kwh-battery/' 
'Examining Tesla Model 3 Production Goals – Are Targets Even Feasible?' 
'Tesla CEO Elon Musk notes a potential of factory production speed improvement by a factor of 10. Where does this put Model 3 production, and at what point might Tesla achieve this monumentally lofty goal? The real answer may be “never”, that is until Tesla has more than a single…' 
'http://insideevs.com/examining-tesla-model-3-production-goals/' 
'All-Electric Class 5 Work Truck With 100 Miles Range To Arrive This Fall' 
'Chanje is a new company based out of Los Angeles,\xa0California, that intends to introduce an all-electric medium-duty vehicle on a mass scale in the U.S., promising first deliveries in 2017. The company is related to Hong Kong based FDG Electric Vehicles, which together with other partners have reportedly invested nearly…' 
'http://insideevs.com/all-electric-class-5-work-truck-with-100-miles-range-to-arrive-this-fall/' 
'Volkswagen CEO Admits Tesla Has Abilities It Lacks' 
'It seems Volkswagen CEO Herbert Diess isn’t quite sure what to say about Tesla. About a month ago, we shared that Diess (whose personal car is a VW eGolf) believes Volkswagen can stop Tesla. His reasoning behind the statement was simply\xa0VW has abilities that Tesla doesn’t possess. Of course, this…' 
'http://insideevs.com/volkswagen-ceo-admits-tesla-ahead/' 
'Tesla Model 3 Sighting In New Zealand – Video' 
'It’s winter over there, so why not conduct some winter testing? This isn’t the first time we’ve seen a Model 3 in New Zealand and likely won’t be the last. Imagine being in New Zealand and spotting a Model 3 prior to anyone outside of the U.S. That’s brag-worthy for…' 
'http://insideevs.com/tesla-model-3-sighting-new-zealand-video/' 
関連する問題