1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python beautifulsoup requests glob find_all

Last updated at Posted at 2020-03-25

###サンプルコード1 (urlを指定)

import requests
from bs4 import BeautifulSoup

url = 'https://xxx'
r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')

#pタグのテキストを表示
tag_p = soup.find_all('p') 
for p in tag_p:
  print(p.text)

#--- 以下は、find_allメソッドの例(findメソッドも同じ) ---
#属性の指定
ids = soup.find_all(id='sample')

#属性の指定(class)
clss = soup.find_all(class_='sample')

#タグ名と属性を指定
divs = soup.find_all('div', class_='sample')

#複数のタグ
tags = soup.find_all(['a', 'b', 'c'])

サンプルコード2 (ファイルを指定)


from glob import glob
from bs4 import BeautifulSoup

# 同一ディクトリ内のhtmファイルを対象とする時
files = glob('*.htm')

for file in files:
  ff = open( file, 'r' ,encoding='utf-8' ).read() 
  soup = BeautifulSoup( ff ,'html.parser')

  #pタグのテキストを表示
  tag_p = soup.find_all('p')
  for p in tag_p:
    print(p.text)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?