自身が
オープンつぶやきのURL、お好みのHTMLパーサに渡すと、あなたが興味を持っているXPathを抽出こすり
スクレイピングがで議論されています。http://docs.python-guide.org/en/latest/scenarios/scrape/
XPathは、Inspectorで強調表示された行を右クリックし、サイトの構造が常に同じ場合は "Copy"> "XPathをコピー"を選択して、必要な要素を右クリックして "Inspect"を選択することで取得できます。それ以外の場合は、目的のオブジェクトを正確に定義するプロパティを選択します。あなたのケースでは
:
//div[contains(@class, 'permalink-tweet-container')]//strong[contains(@class, 'fullname')]/text()
はあなたに作者の名前を取得し、
//div[contains(@class, 'permalink-tweet-container')]//p[contains(@class, 'tweet-text')]//text()
ますが、あなたにツイートの内容を取得します。
フル実施例:中
from lxml import html
import requests
page = requests.get('https://twitter.com/UniteAlbertans/status/899468829151043584')
tree = html.fromstring(page.content)
tree.xpath('//div[contains(@class, "permalink-tweet-container")]//p[contains(@class, "tweet-text")]//text()')
結果:
['Breaking:\n10 sailors missing, 5 injured after USS John S. McCain collides with merchant vessel near Singapore...\n\n', 'https://www.', 'washingtonpost.com/world/another-', 'us-navy-destroyer-collides-with-a-merchant-ship-rescue-efforts-underway/2017/08/20/c42f15b2-8602-11e7-9ce7-9e175d8953fa_story.html?utm_term=.e3e91fff99ba&wpisrc=al_alert-COMBO-world%252Bnation&wpmk=1', u'\xa0', u'\u2026', 'pic.twitter.com/UiGEZq7Eq6']
だけで使用するXPathを明確にする... '//' - どこでもdivの 'の検索[、(@classが含まれています'permalink-tweet-container')] ' - class 'permalink-tweet-container'' '//そして' 'strong [contains(@class、 'fullname')]' - クラスを含むstrong 'fullname' '/' - そこから直接 'text()' - テキストを取得します。 – petrpulc
あなた自身のXPathを、例えばhttp://videlibri.sourceforge.net/cgi-bin/xidelcgi – petrpulc
でテストすることができます。これがあなたの質問に答えるなら、それを受け入れてください。 – petrpulc