LoginSignup
19
11

More than 3 years have passed since last update.

beautifulsoup4 Xpath指定で要素を取得

Last updated at Posted at 2020-01-30

やり方

  • lxmlを使えばOK
    • beautifulsoup単体ではXpath指定不可

流れ

  1. beautifulsoupでhtmlをパースして、
  2. lxmlで変換すると、
  3. xpath指定できる!
import urllib2
from lxml import html
from bs4 import BeautifulSoup

data = urllib2.urlopen("http://example.com")
soup_parsed_data = BeautifulSoup(data, 'html.parser')

lxml_coverted_data = html.fromstring(str(soup_parsed_data))

# あとはお好きにどうぞ
lxml_coverted_data.xpath('xxx/yyy/...')

Xpath書き方

19
11
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
19
11