logo

Python Webスクレイピングを使用して選択したWebページコンテンツを読み取る

前提条件: Python でのファイルのダウンロード BeautifulSoup を使用した Web スクレイピング Python が非常に簡単なプログラミング言語であることは誰もが知っていますが、Python を優れたものにしているのは、Python 用に作成されたオープンソース ライブラリが多数あることです。 Requests は、最も広く使用されているライブラリの 1 つです。これにより、任意の HTTP/HTTPS Web サイトを開いて、Web 上で通常行うあらゆる種類の操作が可能になり、セッション (Cookie) を保存することもできます。誰もが知っているように、Web ページは Web サーバーからブラウザに送信される HTML コードの一部であり、ブラウザによって美しいページに変換されます。次に、HTML ソース コードを取得するメカニズム、つまり BeautifulSoup と呼ばれるパッケージを使用して特定のタグを見つけるメカニズムが必要です。 インストール:
pip3 install requests 
pip3 install beautifulsoup4 

ニュースサイトを読んだ例を見てみましょう ヒンドゥスタン・タイムズ

コードは 3 つの部分に分けることができます。
  • Webページのリクエスト
  • タグの検査
  • 適切な内容を印刷する
手順:
    Webページをリクエストする:まず、ニューステキストを右クリックしてソースコードを表示します。 Python Webスクレイピングを使用して選択したWebページコンテンツを読み取る' title= タグの検査:ソースコードのどの本体にスクラップしたいニュースセクションが含まれているかを把握する必要があります。これは、ニュース セクションを含む、uli.e の順序なしリスト「searchNews」の下にあります。 Python Webスクレイピングを使用して選択したWebページコンテンツを読み取る' title= 注: ニュース テキストはアンカー タグのテキスト部分に存在します。よく観察すると、すべてのニュースは順序付けされていないタグの li リスト タグの中にあるという考えが得られます。 Python Webスクレイピングを使用して選択したWebページコンテンツを読み取る' title= 適切な内容を出力します。 The content is printed with the help of code given below. Python
    import requests from bs4 import BeautifulSoup def news(): # the target we want to open  url='http://www.hindustantimes.com/top-news' #open with GET method resp=requests.get(url) #http_respone 200 means OK status if resp.status_code==200: print('Successfully opened the web page') print('The news are as follow :-n') # we need a parserPython built-in HTML parser is enough . soup=BeautifulSoup(resp.text'html.parser') # l is the list which contains all the text i.e news  l=soup.find('ul'{'class':'searchNews'}) #now we want to print only the text part of the anchor. #find all the elements of a i.e anchor for i in l.findAll('a'): print(i.text) else: print('Error') news() 

    出力

    Successfully opened the web page The news are as follow :- Govt extends toll tax suspension use of old notes for utility bills extended till Nov 14 Modi Abe seal historic civil nuclear pact: What it means for India Rahul queues up at bank says it is to show solidarity with common man IS kills over 60 in Mosul victims dressed in orange and marked 'traitors' Rock On 2 review: Farhan Akhtar Arjun Rampal's band hasn't lost its magic Rumours of shortage in salt supply spark panic among consumers in UP Worrying truth: India ranks first in pneumonia diarrhoea deaths among kids To hell with romance here's why being single is the coolest way to be India vs England: Cheteshwar Pujara Murali Vijay make merry with tons in Rajkot Akshay-Bhumi SRK-Alia Ajay-Parineeti: Age difference doesn't matter anymore Currency ban: Only one-third have bank access; NE backward regions worst hit Nepal's central bank halts transactions with Rs 500 Rs 1000 Indian notes Political upheaval in Punjab after SC tells it to share Sutlej water Let's not kid ourselves with Trump what we have seen is what we will get Want to colour your hair? Try rose gold the hottest hair trend this winter 

参考文献



クイズの作成