#author("2023-02-12T08:50:25+00:00","default:hotate","hotate") #author("2023-06-04T00:11:14+00:00;2023-02-12T08:50:25+00:00","default:hotate","hotate") #contents &tag(スクレイピング, Python); &tag(スクレイピング,Python); * 情報 [#x2e47584] - [[10分で理解する Beautiful Soup - Qiita>https://qiita.com/Chanmoro/items/db51658b073acddea4ac]] HTML や XML から狙ったデータを抽出するためのライブラリです。 - [[[Python入門]Beautiful Soup 4によるスクレイピングの基礎 (1/2):Python入門 - @IT>https://www.atmarkit.co.jp/ait/articles/1910/18/news015.html]] Beautiful Soupは今いったような「HTMLファイルやXMLファイルからデータを抽出するためのPythonライブラリ」だ - [[Beautiful Soup Documentation — Beautiful Soup 4.9.0 documentation>https://www.crummy.com/software/BeautifulSoup/bs4/doc/#get-text]] If you only want the human-readable text inside a document or tag, you can use the get_text() method. It returns all the text in a document or beneath a tag, as a single Unicode string: -- コメント:下記のオプションを使うと更に良い結果が得られた。(2021/01/10) You can tell Beautiful Soup to strip whitespace from the beginning and end of each bit of text: # soup.get_text("|", strip=True) ** [[XML]] [#a1b7d55d] - [[BeautifulsoupでXMLからテキスト取得 - エイエイレトリック>https://eieito.hatenablog.com/entry/2019/10/05/203105]] PythonのBeautifulsoupを使って、国立国会図書館APIから取得したXMLからタイトル情報を取得する。 - [[BeautifulSoupを使ったXMLの解析 – Helve Tech Blog>https://helve-blog.com/posts/python/beautifulsoup-xml-parse/]] BeautifulSoupで使用できるパーサを次表に示す。今回はxmlパーサを使用する。 * [[トラブル]] [#ae27aaf5] ** [[Lambda/Python]] [#l4cc72ea] - [[Python3.7ランタイムのAWS LambdaでC拡張ライブラリを使用したい! - Qiita>https://qiita.com/rkhcx/items/999fe28b541334cd8464]] 調べたところ、どうやらlxmlライブラリはC言語の拡張を使用しており、そのままではlambdaで使えないとのこと。 - [[LambdaでPythonのlxmlを使う - 万年素人からHackerへの道>https://shinriyo.hateblo.jp/entry/2018/07/09/Lambda%E3%81%A7Python%E3%81%AElxml%E3%82%92%E4%BD%BF%E3%81%86]] Pythonだけでなく、OS依存っぽいので面倒ですね。 ** [[XML]] [#hee2df43] - [[BeautifulSoup のエラー "Couldn't find a tree builder" の原因と対処法 - Qiita>https://qiita.com/PND/items/06e1053eeed69ec4f418]] html.parser だけが標準で含まれています。 lxml などは BeautifulSoup の依存パッケージに含まれないので、別途インストールが必要です。 ** 文字化け [#he37a3b4] - [[RequestsとBeautiful Soupでのスクレイピング時に文字化けを減らす - orangain flavor>https://orangain.hatenablog.com/entry/encoding-in-requests-and-beautiful-soup]] 以下の2点を守ると概ね幸せです。 Content-Typeヘッダーのエンコーディングを参照するコードは下の方に掲載しています。 -- コメント:Requestsの出力時点での文字コードを確認すると良い。([[Python>Python#p42ddee5]] 2023/02/12) * 関連 [#pd6dd7cb] #related #taglist(tag=スクレイピング) - [[Python]]