给定这个HTML代码段,我如何使用python包请求或xlml来查找href=之后引用的字符串?你知道吗
<dl>
<dt><a href="oq-phys.htm">
<b>Physics and Astronomy</b></a>
<dt><a href="oq-math.htm">
<b>Mathematics</b></a>
<dt><a href="oq-life.htm">
<b>Life Sciences</b></a>
<dt><a href="oq-tech.htm">
<b>Technology</b></a>
<dt><a href="oq-geo.htm">
<b>Earth and Environmental Science</b></a>
</dl>
对于上面的示例,假设我们有包含上述代码段的html\u字符串。你知道吗
for quoted_link in html_string.xpath('//a'): print(quoted_link.attrib['href'], quoted_link.text_content())
有很多方法可以剥这只猫的皮。下面是一个
requests
/lxml
解决方案,它不包含(显式)for
循环:编辑
我为什么这样写:
基准:
输出:
短
requests
+beautifulsoup
溶液:输出:
相关问题 更多 >
编程相关推荐