求助 用 xpath 如何取到 title 里的文字啊

2017-12-30 00:10:15 +08:00
 F1024
<div class="item-pic">
<a href="//2.taobao.com/item.htm?id=560088094729" target="_blank" title=" [转卖] 创得 小米 5 手机壳小米 5s 保护套小米 6 防摔硅</a>
</div>

大佬们用 xpath 如何取到 title 里的文字啊?

import os
import requests
from lxml import etree

res = requests.get('https://s.2.taobao.com/list/list.htm?_input_charset=utf8&q=小米 6&st_edtime=1').content
txt = etree.HTML(res)
txt2 = txt.xpath('//div/a/text()')

for tt in txt2:
print(tt)

os.system("pause")
这样取不到
4517 次点击
所在节点    Python
2 条回复
dd0754
2017-12-30 01:27:47 +08:00
```python
import os
import requests
from lxml import etree

res = requests.get('https://s.2.taobao.com/list/list.htm?_input_charset=utf8&q=小米 6&st_edtime=1').content
txt = etree.HTML(res)
items = txt.xpath('//div[@class="ks-waterfall"]')
for item in items[1:]:
print(item.attrib)
a = item.xpath('./div/div[@class="item-info"]/div/a')
for d in a:
print(d.attrib)

```
F1024
2017-12-30 10:44:10 +08:00
@dd0754
import requests
from lxml import etree

res = requests.get('https://s.2.taobao.com/list/list.htm?_input_charset=utf8&q=ddr3 1866&st_edtime=1').content
txt = etree.HTML(res)

txt4 = txt.xpath('//div/div[@class="item-info"]/div/a')

print(txt4[0].attrib.get('title'))
可以了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/418767

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX