-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathspider.py
112 lines (105 loc) · 4.1 KB
/
spider.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
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
import requests,re,os
from hashlib import md5
from selenium import webdriver
def get_cookies(url):
str=''
options = webdriver.ChromeOptions()
options.add_argument('--headless')
browser = webdriver.Chrome(options=options)
browser.get(url)
for i in browser.get_cookies():
try:
name=i.get('name')
value=i.get('value')
str=str+name+'='+value+';'
except ValueError as e:
print(e)
return str
def get_page(offset):
params = {
'aid': '24',
'app_name': 'web_search',
'offset': offset,
'format': 'json',
'keyword': '街拍',
'autoload': 'true',
'count': '20',
'en_qc': '1',
'cur_tab': '1',
'from': 'search_tab',
'pd': 'synthesis',
}
url='https://www.toutiao.com/api/search/content/'
try:
r=requests.get(url,params=params,headers=headers)
if r.status_code==200:
return r.json()
else:
print('requests get_page error!')
except requests.ConnectionError:
return None
def get_images(json):
data=json.get('data')
if data:
for i in data:
if i.get('title'):
title=re.sub('[\t]','',i.get('title'))
url=i.get('article_url')
if url:
r=requests.get(url,headers=headers)
if r.status_code==200:
images_pattern = re.compile('JSON.parse\("(.*?)"\),\n', re.S)
result = re.search(images_pattern, r.text)
if result:
b_url='http://p3.pstatp.com/origin/pgc-image/'
up=re.compile('url(.*?)"width',re.S)
results=re.findall(up,result.group(1))
if results:
for result in results:
yield {
'title':title,
'image':b_url+re.search('F([^F]*)\\\\",',result).group(1)
}
else:
images = i.get('image_list')
for image in images:
origin_image = re.sub("list.*?pgc-image", "large/pgc-image",
image.get('url')) # 改成origin/pgc-image是原图
yield {
'image': origin_image,
'title': title
}
def save_image(item):
img_path = 'img' + os.path.sep + item.get('title')
if not os.path.exists(img_path):
os.makedirs(img_path) # 生成目录文件夹
try:
resp = requests.get(item.get('image'))
if requests.codes.ok == resp.status_code:
file_path = img_path + os.path.sep + '{file_name}.{file_suffix}'.format(
file_name=md5(resp.content).hexdigest(),
file_suffix='jpg') # 单一文件的路径
if not os.path.exists(file_path):
with open(file_path, 'wb') as f:
f.write(resp.content)
print('Downloaded image path is %s' % file_path)
else:
print('Already Downloaded', file_path)
except Exception as e:
print(e,'none123')
def main(offset):
a = get_page(offset)
for i in get_images(a):
save_image(i)
cookies = get_cookies('https://www.toutiao.com')
headers = {
'cookie': cookies,
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
'referer': 'https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D',
}
if __name__=='__main__':
#p.map(main,[0]) #之所以不用Pool多进程是因为目前还没有办法实现跨进程共享Cookies
#map(main,[x*20 for x in range(3)]) map没有输出,不知道为什么
for i in [x*20 for x in range(3)]:
main(i)