直接上代码哦,够直接了吧~

from lxml import etree
#import mechanize
import lxml.html
#import cookielib

#br = mechanize.Browser()
#r = br.open('http://yourdomain.com')
#html = br.response().read()
#root = lxml.html.fromstring(html)
#divs = root.xpath("//div[@class='test']")
hparser = etree.HTMLParser(encoding='utf-8') #for avoiding unicode codec problems
htree = etree.parse('http://yourdomain.com',hparser)
htree.write('/tmp/bi.html') 
divs= htree.xpath("//div[@class='test']")

要获取class包含test的所有div,比如<div class="test website"></div> 

把上述xpath的参数修改为 "div[contains(@class,'test')]" 即可。

相关文章:

  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
猜你喜欢
  • 2021-12-07
  • 2021-04-30
  • 2021-11-17
  • 2021-12-22
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案