[笔记]PYTHON_xpath三元运算符的应用
直接上代码:import requests,re
from lxml import etree
class Xiurenji:
def __init__(self):
self.host='https://m.xiurenji.cc'
self.headers={
'Referer': 'https://m.xiurenji.cc/XiuRen/',
'User-Agent': 'Mozilla/5.0 (Linux; Android 9; DUK-AL20 Build/HUAWEIDUK-AL20; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/79.0.3945.116 Mobile Safari/537.36'}
def daohang_parser(self):
lst_title,lst_href=[],[]
daohang_url='https://m.xiurenji.cc/daohang.html'
req=requests.get(daohang_url,headers=self.headers)
html_str=req.content.decode('gbk')
#print(html_str)
_etree=etree.HTML(html_str)
new_top100_title=_etree.xpath('//table/td/a/text()')
for i in new_top100_title:lst_title.append(i)
#print(new_top100_title)
new_top100_href=_etree.xpath('//table/td/a/@href')
for i in new_top100_href:lst_href.append(self.host+i)
#print(new_top100_href)
list_title=_etree.xpath('//div/div/a/text()')
for i in list_title:lst_title.append(i)
list_href=_etree.xpath('//div/div/a/@href')
for i in list_href:lst_href.append(self.host+i)
#print(lst_title,lst_href)
print('»»» Www.XiuRenJi.Com秀人集-导航 »»»')
print( '本站收集秀人旗下全系列共23个机构写真!')
for i in range(len(lst_title)):print(i,lst_title)
flage=True
while flage:
select=input('\n»»» 选择new,top,23家的写真机构:')
try:
select=int(select) if int(select) in range(25) else print('»»» Range Enter Error, Run Again!')
flage=False if select in range(25) else True
'''if int(select) in range(25):
select,flage=int(select),False
else:
print('»»» Range Enter Error, Run Again!')
'''
except ValueError:
print('ValueError ,pls run again!')
return select,lst_title,lst_href
def run(self):
#1.daohuang
self.daohang_parser()
#2.part list
if __name__=='__main__':
xrj=Xiurenji()
xrj.run()
页:
[1]