学习中心
登录
通过xpath 爬取数据为空?
import requests
from lxml import etree

url = 'https://www.biquge.name/class1/'
headers= {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'}
resp = requests.get(url,headers)
e= etree.HTML(resp.text) #将str类型转换为etree.element 类型
print(type(e))
names= e.xpath('//*[@id="hotcontent"]/div/div[2]/dl/dt/a')
authors = e.xpath('//*[@id="hotcontent"]/div/div[2]/dl/dt/span')
print(names)
for name,author in zip(names,authors):
    print(name,":",author)
# //*[@id="hotcontent"]/div/div[1]/dl/dt/a

53e59dd775434d6353053c0dd1d68f39.png

70 1
    1个回答
    你还没有查看该回答的权限哦~请先获取查看权限
    立即查看
    写回答