-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathc01.py
31 lines (23 loc) · 985 Bytes
/
c01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# coding=utf-8
import requests
from lxml import etree
import numpy as np
base_url = 'https://spiderbuf.cn/web-scraping-practice/scraper-practice-c01/mnist'
my_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36',
'Referer': 'https://spiderbuf.cn/web-scraping-practice/c01',
'Cookie': '__cgf3t=G0gzgFKDRlLtmZH7NrzqOb1x4pek1xNQk12KKc4g21Y-1731624199;'}
html_bytes = requests.get(base_url, headers=my_headers).content
html = html_bytes.decode()
root = etree.HTML(html)
with open('./data/c01/c01.html', 'w', encoding='utf-8') as f:
f.write(html)
# print(html)
trs = root.xpath('//tbody/tr')
pix1_arry = []
for tr in trs:
tds = tr.xpath('td')
# 把 pix1 列的值添加到数组
pix1_arry.append([int(tds[1].text) if len(tds) > 1 else 0])
# 计算 pix1 列的平均值并四舍五入至两位小数
print(round(np.mean(pix1_arry),2))