-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhupuexample.py
More file actions
127 lines (110 loc) · 2.96 KB
/
hupuexample.py
File metadata and controls
127 lines (110 loc) · 2.96 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from bs4 import BeautifulSoup
import chardet
import requests
import pprint
import json
import pandas as pd
import numpy as np
'''
creat beautifulsoup
'''
def creat_bs(url):
result = requests.get(url)
e=chardet.detect(result.content)['encoding']
#set the code of request object to the webpage's code
result.encoding=e
c = result.content
soup=BeautifulSoup(c,'lxml')
return soup
'''
build urls group
'''
def build_urls(prefix,suffix):
urls=[]
for item in suffix:
url=prefix+item
urls.append(url)
return urls
'''
acquire all the page titles and links and save it
'''
def find_title_link(soup):
titles=[]
links=[]
try:
contanier=soup.find('div',{'class':'container_padd'})
ajaxtable=contanier.find('form',{'id':'ajaxtable'})
page_list=ajaxtable.find_all('li')
for page in page_list:
titlelink=page.find('a',{'class':'truetit'})
if titlelink.text==None:
title=titlelink.find('b').text
else:
title=titlelink.text
if np.random.uniform(0,1)>0.90:
link=titlelink.get('href')
titles.append(title)
links.append(link)
except:
print('have none value')
return titles,links
'''
acquire reply in every topic of 10 page
'''
def find_reply(soup):
replys=[]
try:
details=soup.find('div',{'class':'hp-wrap details'})
form=details.find('form')
floors=form.find_all('div',{'class':'floor'})
for floor in floors:
table=floor.find('table',{'class':'case'})
if floor.id!='tpc':
if table.find('p')!=None:
reply=table.find('p').text
else:
reply=table.find('td').text
replys.append(reply)
elif floor.id=='tpc':
continue
except:
return None
return replys
'''
acquire information from hupu pubg bbs
'''
url='https://bbs.hupu.com/pubg'
page_suffix=['','-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']
urls=build_urls(url,page_suffix)
title_group=[]
link_group=[]
for url in urls:
soup=creat_bs(url)
titles,links=find_title_link(soup)
for title in titles:
title_group.append(title)
for link in links:
link_group.append(link)
reply_urls=build_urls('https://bbs.hupu.com',link_group)
reply_group=[]
for url in reply_urls:
soup=creat_bs(url)
replys=find_reply(soup)
if replys!=None:
for reply in replys:
reply_group.append(reply)
'''
creat wordlist and save as txt
'''
wordlist=str()
for title in title_group:
wordlist+=title
for reply in reply_group:
wordlist+=reply
def savetxt(wordlist):
f=open('wordlist.txt','wb')
f.write(wordlist.encode('utf8'))
f.close()
savetxt(wordlist)