11管宵、實戰(zhàn)3:爬取今日頭條街拍圖片

附源碼:

import requests

from urllib.parse import urlencode

import re

import os

from requests import codes

from hashlib import md5

from multiprocessing.pool import Pool

def get_page(offset):

? ? headers = {

? ? ? ? 'cookie': 'tt_webid=6726380411449148935; WEATHER_CITY=%E5%8C%97%E4%BA%AC; __tasessionId=385zzngu11566107488919; tt_webid=6726380411449148935; csrftoken=7bf3e286e8095f9371dc5e2bfab0ed19; s_v_web_id=fb560f45a8b77ca1a01cc1c4abb0e9b1',

? ? ? ? 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',

? ? ? ? 'x-requested-with': 'XMLHttpRequest',

? ? ? ? 'referer': 'https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D'

? ? }

? ? 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'

? ? }

? ? base_url = 'https://www.toutiao.com/api/search/content/?'

? ? url = base_url + urlencode(params)

? ? # print(url)

? ? try:

? ? ? ? resp = requests.get(url, headers = headers)

? ? ? ? if resp.status_code == 200:

? ? ? ? ? ? return resp.json()

? ? except requests.ConnectionError:

? ? ? ? return None

def get_images(json):

? ? if json.get('data'):

? ? ? ? data = json.get('data')

? ? ? ? for item in data:

? ? ? ? ? ? if item.get('title') is None:

? ? ? ? ? ? ? ? continue

? ? ? ? ? ? title = re.sub('[\t]', '', item.get('title'))

? ? ? ? ? ? images = item.get('image_list')

? ? ? ? ? ? if images is None:

? ? ? ? ? ? ? ? continue

? ? ? ? ? ? for image in images:

? ? ? ? ? ? ? ? origin_image = re.sub('list.*?pgc-image', 'large/pgc-image', image.get('url'))

? ? ? ? ? ? ? ? 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 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)

def main(offset):

? ? json = get_page(offset)

? ? for item in get_images(json):

? ? ? ? save_image(item)

GROUP_START = 0

GROUP_END = 0

if __name__ == '__main__':

? ? pool = Pool()

? ? groups = ([x * 20 for x in range(GROUP_START, GROUP_END + 1)])

? ? pool.map(main, groups)

? ? pool.close()

? ? pool.join()

爬取效果:

爬取效果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末恃锉,一起剝皮案震驚了整個濱河市论熙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌禁炒,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件霍比,死亡現(xiàn)場離奇詭異幕袱,居然都是意外死亡,警方通過查閱死者的電腦和手機桂塞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門凹蜂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人阁危,你說我怎么就攤上這事玛痊。” “怎么了狂打?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵擂煞,是天一觀的道長。 經(jīng)常有香客問我趴乡,道長对省,這世上最難降的妖魔是什么蝗拿? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮蒿涎,結(jié)果婚禮上哀托,老公的妹妹穿的比我還像新娘。我一直安慰自己劳秋,他們只是感情好仓手,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著玻淑,像睡著了一般嗽冒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上补履,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天添坊,我揣著相機與錄音,去河邊找鬼箫锤。 笑死贬蛙,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的麻汰。 我是一名探鬼主播速客,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼五鲫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起岔擂,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤位喂,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后身弊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體孝常,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡侵俗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了规婆。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡蝉稳,死狀恐怖抒蚜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情耘戚,我是刑警寧澤嗡髓,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站收津,受9級特大地震影響饿这,放射性物質(zhì)發(fā)生泄漏浊伙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一长捧、第九天 我趴在偏房一處隱蔽的房頂上張望嚣鄙。 院中可真熱鬧,春花似錦串结、人聲如沸哑子。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赵抢。三九已至,卻和暖如春声功,著一層夾襖步出監(jiān)牢的瞬間烦却,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工先巴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留其爵,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓伸蚯,卻偏偏與公主長得像摩渺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子剂邮,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容