Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

web scraping - PHP: Simple HTML DOM Parser - multiple attributes in find()?

I'm using Simple HTML DOM Parser but can't figure out how to get elements that have 2 or more matching attributes.

Sadly, to get the first 2 divs, this doesn't work:

$html = "<div title='test a' class='a' >test a</div>
         <div title='test b' class='b' >test b</div>
         <div title='test c' class='c' >test c</div>";

$htmldom = str_get_html($html);
$ab = $htmldom->find("div[class=a][class=b]");

Is it even possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can find all divs with either class a or class b by invoking:

$ab = $htmldom->find("div[class=a], div[class=b]");

For details, see manual on "How to find HTML elements -> Advanced".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...