爬虫代码已复制

S01 - requests库及lxml库入门

1718093235阅读数:328
上一篇:Python调用Selenium爬取网页
下一篇:S02 - http请求分析及头构造使用
# coding=utf-8

import requests
from lxml import etree

url = 'https://www.spiderbuf.cn/playground/s01'

html = requests.get(url).text

f = open('01.html', 'w', encoding='utf-8')
f.write(html)
f.close()

root = etree.HTML(html)
trs = root.xpath('//tr')

f = open('data01.txt', 'w', encoding='utf-8')
for tr in trs:
    tds = tr.xpath('./td')
    s = ''
    for td in tds:
        # print(td.text)
        s = s + str(td.text) + '|'
    print(s)
    if s != '':
        f.write(s + '\n')

f.close()

# print(html)