 Sometime during Emperor Xizong's "Zhonghe" era (881-885), when Emperor Xizong had fled to Chengdu due to the major agrarian rebellion led by Huang Chao, who captured Chang'an and declared his own state of Qi as its emperor, Emperor Xizong, in order to raise funds for the military campaigns against Qi, moved the office of salt and iron monopolies to Yang Prefecture (揚州, in modern Yangzhou, Jiangsu) and put the official Li Du (李都) in charge of the office.At Li Du's recommendation, Zhang Wenwei was made a roving surveyor for the office.After Huang was defeated and Emperor Xizong returned to Chang'an in 885, Zhang was first made an imperial censor with the title "Jiancha Yushi" (監察御史); then (either successively or holding the offices all at once) "Zuo Bujue" (左補闕), a low-level advisory official at the examination bureau of government (門下省, "Menxia Sheng"); an imperial censor with the title "Dianzhong Shi Yushi" (殿中侍御史); an imperial chronicler with the title "Qiju Sheren" (起居舍人); and "Sixun Yuanwailang" (司勛員外郎), a low-level official at the ministry of civil service affairs (吏部, "Libu").