前提・実現したいこと
ニュース記事をスクレイピングをする際の,CSSセレクタの指定方法を教えていただきたいです.
ニュース記事の文章のみをPythonのscrapyを用いてスクレイピングしようとしているのですが,文章中にハイライトがある部分では別のテキストとして認識されて,抽出されるときにも別の要素として配列に格納されます.
例えば,以下のテキストにおいて
Rob Leathern, director of product management at Facebook, said the company will update its disclosure policy in Britain next month. It will require political advertisers to verify their identities and then attach accurate information about their identities to the ads.
The changes are part of new political advertising policies that Facebook announced this week for users in Britain. No only will political ads need to be more clearly labeled, but the company is establishing a searchable archive of political ads that have been published on the site.
"Rob Leathern ... ads"で一つの配列要素
"The changes are part of"で一つの配列要素
"new political advertising"で一つの配列要素
"that ... site."で一つの配列要素
として,
Python
1["Rob Leathern ... ads", "The changes are part of", "new political advertising", "that ... site."]
というように配列に格納されてしまいます.
理想的には
Python
1["Rob Leathern ... ads", "The changes are part of ... new political advertising ... that ... site."]
というように分割されないように格納したいと思っております.
段落わけや,改行によって文章要素が分割されて,別の要素として格納される分には問題ないのですが,1文中の単語だけがあたかも段落わけや改行されてしまうのは都合が悪いのです.
そのため,a タグ等単語のハイライト等を無視でき
p タグ以下に含まれている文章全てを結合させた形で抽出できるような
CSSセレクタの指定方法をご教授頂きたいです.
どうぞよろしくお願い致します。
発生している問題・エラーメッセージ
<div class="css-18sbwfn StoryBodyCompanionColumn"> <div class="css-4w7y5l"> <p class="css-1xl4flh e2kc3sl0"> Rob Leathern, director of product management at Facebook, said the company will update its disclosure policy in Britain next month. It will require political advertisers to verify their identities and then attach accurate information about their identities to the ads. </p> </div> <aside class="css-14jsv4e"> <span> </span> </aside> </div> <div class="css-18sbwfn StoryBodyCompanionColumn"> <div class="css-4w7y5l"> <p class="css-1xl4flh e2kc3sl0"> The changes are part of <a class="css-1g7m0tk" href="https://newsroom.fb.com/news/2018/10/increasing-transparency-uk/" title="" rel="noopener noreferrer" target="_blank"> new political advertising policies </a> that Facebook announced this week for users in Britain. No only will political ads need to be more clearly labeled, but the company is establishing a searchable archive of political ads that have been published on the site. </p> </div> <aside class="css-14jsv4e"> <span> </span> </aside> </div>
該当のソースコード
以下のコードにあるCSSセレクタの指定書式では分割されてしまいます.
Python
1body = response.css('article#story section div *::text').extract() 2print(body)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。