开发者

beautifulsoup find text with and without regex

The html:

<td>some key
</td>

find without regex:

soup.find(text='some key')

returned None

find with regex

soup.find(text=re.compile('some key'))

returned the td node.

Would anyone point out the difference between the two approaches? "some key" is a literal string without special characters. I noted that there's a carriage return at the end 开发者_运维百科of "some key" that </td> appears on the next line.

Thank you.


Beautifulsoup uses == to match the content between tags and the search string. Since 'some key\r\n' != 'some key', the search failed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜