url = f"{target}/s01" html = requests.get(url).text
f = open('S01.html', 'w', encoding='utf-8') f.write(html) f.close()
root = etree.HTML(html) trs = root.xpath('//tr') for tr in trs: tds = tr.xpath('./td') s = '' for td in tds: s = s + td.text + '|' print(s)
格式化输出td的信息,利用空串拼接,但td列表存在空数据,利用str()显式转换去处理。
PYTHON - 4 lines
1 2 3 4
Traceback (most recent call last): File "D:\PyBatch\cyber\S01.py", line 19, in <module> s = s + td.text + '|' TypeError: can only concatenate str (not"NoneType") to str
url = f"{target}/s01" html = requests.get(url).text
f = open('S01.html', 'w', encoding='utf-8') f.write(html) f.close()
f = open('datas01.txt', 'w', encoding='utf-8') root = etree.HTML(html) trs = root.xpath('//tr') for tr in trs: tds = tr.xpath('./td') s = '' for td in tds: s = s + str(td.text) + '|' print(s) if s != '': f.write(s + '\n') f.close()