Where it's @
在本练习中,您将开始编写一个使用属性的 XPath 字符串,以完成一个特定任务:选中包含文本 "Thanks for Watching!" 的段落元素。我们已经为您创建了大部分 XPath 字符串。
请查看以下 HTML:
<html>
<body>
<div id="div1" class="class-1">
<p class="class-1 class-2">Hello World!</p>
<div id="div2">
<p id="p2" class="class-2">Choose DataCamp!</p>
</div>
</div>
<div id="div3" class="class-2">
<p class="class-2">Thanks for Watching!</p>
</div>
</body>
</html>
我们已为您创建了函数 print_element_text(),它会打印所选元素中包含的任何文本。
本练习是课程的一部分
Python Web 爬取
练习说明
- 填写 XPath 字符串中的空白,选中包含短语 "Thanks for Watching!" 的段落元素。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create an Xpath string to select desired p element
xpath = '//*[@id=____]/p'
# Print out selection text
print_element_text( xpath )