我有一个html:
<div class="et_pb_text_inner">
<h3 style="text-align: center;"><i class="fal fa-ruler-combined"></i><br /> 1672 Square Feet</h3>
<p style="text-align: center;">
First Floor 1085 s.f.<br />
Second Floor 587 s.f.<br />
Porches 393 s.f.<br />
Covered Parking 642 s.f.<br />
Storage 187 s.f.<br />
Under Roof 2894 s.f.
</p>
</div>
我的Xpath代码是:
//div[@class='et_pb_text_inner']/p/text()
但它只给我第一行之前的文字。你知道吗
First Floor 1085 s.f.
有没有办法只获取段落元素中的所有数据?有没有办法划清界限?你知道吗
我想得到这样的字符串:
First Floor 1085 s.f.<br />Second Floor 587 s.f.<br />Porches 393 s.f.<br />Covered Parking 642 s.f.<br />Storage 187 s.f.<br />Under Roof 2894 s.f.
所以在那之后我将尝试清理数据。你知道吗
我是xpath表达式的新手。你知道吗
事先谢谢你的帮助。)很抱歉我的英语不好。:页
我的.py代码是:
import requests
from lxml import html
resp = requests.get(
url="https://tyreehouseplans.com/shop/house-plans/beach-house-plans/crew-cut-house-plan/",
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
)
tree = html.fromstring(html=resp.text)
title = tree.xpath("//div[@class='et_pb_module_inner']/h1/text()")[0]
dimensions = tree.xpath("//div[@class='et_pb_text_inner']/p/text()")[0]
print(title)
print(dimensions)
text()
返回一个文本节点,<p>
标记包含6个这样的节点,因此需要删除索引[0]
。您还需要提供更精确的xpath
来删除不需要的结果将给出一个包含6个字符串的列表
相关问题 更多 >
编程相关推荐