菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
118
0

python xpath

原创
05/13 14:22
阅读数 96613

//img/@src 得到所有img标签的src值

//a/text() 得到所有A标签中的文本

name=response.xpath('//img@src').getall()  #getall()从对象中获取具体值

 

import requests,re
from lxml import etree
import json,sys
url1="http://tieba.baidu.com/f?kw=%E6%AD%A6%E6%B1%89&red_tag=d3345989481"
html=requests.get(url1)
select=etree.HTML(html.text)
quyu=select.xpath('//li[@class=" j_thread_list clearfix"]')
#print(quyu)
for each in quyu:
    auth=json.loads(each.xpath('@data-field')[0])
    author=auth['author_name']
    id=auth['id']
    #print(type(auth))
    print(author,id)

 

结果:

==================== RESTART: D:/Python/Python37/tieba.py ====================
silent烟花 6496741920
silent烟花 6497028530
silent烟花 6497025455
silent烟花 6497024605
silent烟花 6497012358
silent烟花 6497001415
silent烟花 6496980363
silent烟花 6496995996
silent烟花 6496995111
silent烟花 6496994178
贴吧官方 6496068827

发表评论

0/200
118 点赞
0 评论
收藏