xpath学习

import requests
from lxml import etree

if __name__=='__main__':
url = 'https://www.runoob.com/python3/python3-examples.html'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38'
}
response = requests.get(url=url,headers=headers).text
tree = etree.HTML(response)
li_list = tree.xpath('//*[@id="content"]/ul/li')
new_urls = []

for li in li_list:

new_url = li.xpath('./a/@href')
new = ''
ur = new.join(new_url)
if '/python3/' not in ur:
ur = '/python3/'+ur
else:
ur = ur
new_url = 'https://www.runoob.com'+ ur
new_urls.append(new_url)

# print(new_url)
for p in new_urls:
page_text = requests.get(url=p,headers=headers).text
tr = etree.HTML(page_text)
title = tr.xpath('//*[@id="content"]/h1/text()')[0]
question = tr.xpath('//*[@id="content"]/p[2]/text()')[0]
print(title +'\n'+question+'\n'+'\n')

numpy:(2条消息) Python之Numpy详细教程a373595475的博客-CSDN博客numpy

pandas:(2条消息) Pandas基础教程_Melo-CSDN博客

matplotlib:(2条消息) Python--Matplotlib(基本用法)苦作舟的人呐-CSDN博客matplotlib

posted @ 2021-10-22 08:27  小毂  阅读(67)  评论(0)    收藏  举报