-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathichunqiu_list.py
77 lines (73 loc) · 3.88 KB
/
ichunqiu_list.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
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
import urllib
import requests
import random
import time
from lxml import etree
from pymongo import MongoClient
my_headers = [ "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
'Opera/9.25 (Windows NT 5.1; U; en)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
"Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7",
"Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0 ",
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36']
headers = {
'user-agent': random.choice(my_headers),
'Connection': 'keep-alive',
'host': 'bbs.ichunqiu.com',
'Upgrade-Insecure-Requests': '1',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Enocding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'referer': 'https://bbs.ichunqiu.com/forum.php',
}
def get_id(page):
url = 'https://bbs.ichunqiu.com/forum.php?mod=forumdisplay&fid=59&orderby=dateline&orderby=dateline&filter=author&page={0}'.format(page)
dict_list = []
page_source = requests.get(url, headers=headers).content
html = etree.HTML(page_source)
content_list = html.xpath("//*[starts-with(@id,'normalthread_')]")
for content in content_list:
post_id = content.xpath("@id")[0].replace('normalthread_', '')
post_url = 'https://bbs.ichunqiu.com/thread-{0}-1-1.html'.format(post_id)
post_time = content.xpath("tr/th/div[2]/i[1]/span/span/@title")
if len(post_time) != 0:
post_time = post_time[0]
else:
post_time = ''
dict = {'_id': post_id,
'post_url': post_url,
'post_time': post_time,
'crawl_id_time': time.time()}
print(dict)
dict_list.append(dict)
return dict_list
def save_mongo(dict_list):
conn = MongoClient('192.168.0.50', 27017)
db = conn.ichunqiu
my_set = db.test_1024
try:
my_set.insert_many(dict_list)
print('******************insert database success!*************************')
except:
for dict in dict_list:
try:
my_set.insert(dict)
except:
print('###################insert database fail!!#######################')
print('******************insert database success!*************************')
if __name__ == '__main__':
page = 1
while page <= 109:
dict_list = get_id(page)
save_mongo(dict_list)
page = page + 1