forked from hhuayuan/spiderbuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s04.py
48 lines (37 loc) · 1.16 KB
/
s04.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# coding=utf-8
import requests
from lxml import etree
import re
base_url = 'http://www.spiderbuf.cn/s04/?pageno=%d'
myheaders = {
'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'}
# 取页数
html = requests.get(base_url % 1, headers=myheaders).text
root = etree.HTML(html)
lis = root.xpath('//ul[@class="pagination"]/li')
page_text = lis[0].xpath('string(.)')
ls = re.findall('[0-9]', page_text)
max_no = int(ls[0])
# exit()
for i in range(1, max_no + 1):
print(i)
url = base_url % i
print(url)
html = requests.get(url, headers=myheaders).text
print(html)
f = open('04_%d.html' % i, 'w', encoding='utf-8')
f.write(html)
f.close()
root = etree.HTML(html)
trs = root.xpath('//tr')
f = open('data04_%d.txt' % i, 'w', encoding='utf-8')
for tr in trs:
tds = tr.xpath('./td')
s = ''
for td in tds:
s = s + str(td.xpath('string(.)')) + '|'
# s = s + str(td.text) + '|'
print(s)
if s != '':
f.write(s + '\n')
f.close()