forked from queensun/Nyspider
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchina10.py
70 lines (63 loc) · 2.2 KB
/
china10.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#coding:utf-8
import requests
from bs4 import BeautifulSoup
import time
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0"}
def get_kinds():
f=open('types.txt','w')
url='http://www.china-10.com/brand/'
html=requests.get(url).text
table=BeautifulSoup(html,'lxml').find('div',id='menubox').find('ul',id='conmenu').find_all('li',attrs={'class':'menu'})
for item in table[1:-3]:
key=item.find('a').get_text().replace('\n','')+'||'
for li in item.find_all('li'):
f.write(key+li.find('a').get('title')+'||'+li.find('a').get('href')+'\n')
f.close()
def get_brands():
f=open('types.txt','r')
data=open('brands.txt','w')
for line in f.readlines():
print(line)
line=line.replace('\n','')
page=1
while True:
html=requests.get(line.split('||')[-1]+'?action=ajax&page='+str(page),headers).text
page+=1
table=BeautifulSoup(html,'lxml').find_all('li')
if(table==[]):
break
for item in table:
text=line+'||'+item.get_text()+'||'+item.find('a').get('href')+'\n'
data.write(text)
print(page)
f.close()
def get_infor(line):
html=requests.get(line.split('||')[-1],headers=headers).text
table=BeautifulSoup(html,'lxml').find('div',attrs={'class':'brandinfo'})
des=table.find('dd').get_text()
line+='||'+des
table=table.find('ul').find_all('li')
for li in table:
line+='||'+li.get_text().replace('\r','').replace('\n','').replace('\t','').replace(' ','')
return line
def main():
data=open('data.txt','a')
failed=open('failed.txt','a')
count=0
for line in open('brands.txt','r').readlines():
line=line.replace('\n','')
try:
line=get_infor(line)
except:
failed.write(line+'\n')
continue
data.write(line+'\n')
count+=1
time.sleep(1)
print(count)
main()