まず持っている、またはしたいデータ項目に隣接している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/'
各項目について、タイトルと略語が必要ですか? –
@BillBellタイトルは素晴らしいでしょう。短縮テキストの意味は? ダイレクトリンクを個別に抽出する方法もありますか? (ごめんなさい、Python初心者です!) – hiimarksman
略語:サムネイル画像の右側に表示される単語は、 '...'で終わっています。はい、あなたは完全な記事へのリンクを得ることもできます。 –