Python3WebSpider/Jiepai

params和图片下载地址的更改,程序又能跑起来啦

zhengbeiandy opened this issue · 33 comments

今天看了下demo由于今日头条中参数的更改导致程序又跑不起来了,更改了下,现在又能跑起来了
import requests
from urllib.parse import urlencode
import os
from hashlib import md5
from multiprocessing.pool import Pool
import re

def get_page(offset):
params = {
'aid':'24',
'offset': offset,
'format': 'json',
'autoload': 'true',
'count': '20',
'cur_tab': '1',
'from': 'search_tab',
'pd':'synthesis'
}
base_url = 'https://www.toutiao.com/api/search/content/?keyword=%E8%A1%97%E6%8B%8D'
url = base_url + urlencode(params)
try:
resp = requests.get(url)
print(url)
if 200 == resp.status_code:
print(resp.json())
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('cell_type') is not None:
continue
title = item.get('title')
images = item.get('image_list')
for image in images:
origin_image = re.sub("list", "origin",image.get('url'))
yield {
'image': origin_image,
'title': title
}

def save_image(item):
img_path = 'img' + os.path.sep + item.get('title')
print(img_path)
if not os.path.exists(img_path):
os.makedirs(img_path)
try:
resp = requests.get(item.get('image'))
if 200 == 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 requests.ConnectionError:
print('Failed to Save Image%s'%item)

def main(offset):
json = get_page(offset)
for item in get_images(json):
print(item)
save_image(item)

GROUP_START = 0
GROUP_END = 7

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

建议楼上使用正确的代码排版

import requests
from urllib.parse import urlencode
from requests import codes
import os
from hashlib import md5
from multiprocessing.pool import Pool
import re


def get_page(offset):
    params = {
        'aid': '24',
        'offset': offset,
        'format': 'json',
        #'keyword': '街拍',
        'autoload': 'true',
        'count': '20',
        'cur_tab': '1',
        'from': 'search_tab',
        'pd': 'synthesis'
    }
    base_url = 'https://www.toutiao.com/api/search/content/?keyword=%E8%A1%97%E6%8B%8D'
    url = base_url + urlencode(params)
    try:
        resp = requests.get(url)
        print(url)
        if 200  == resp.status_code:
            print(resp.json())
            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('cell_type') is not None:
                continue
            title = item.get('title')
            images = item.get('image_list')
            for image in images:
                origin_image = re.sub("list", "origin", image.get('url'))
                yield {
                    'image':  origin_image,
                    # 'iamge': image.get('url'),
                    'title': title
                }

print('succ')

def save_image(item):
    img_path = 'img' + os.path.sep + item.get('title')
    print('succ2')
    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):
                print('succ3')
                with open(file_path, 'wb') as f:
                    f.write(resp.content)
                print('Downloaded image path is %s' % file_path)
                print('succ4')
            else:
                print('Already Downloaded', file_path)
    except requests.ConnectionError:
        print('Failed to Save Image,item %s' % item)


def main(offset):
    json = get_page(offset)
    for item in get_images(json):
        print(item)
        save_image(item)


GROUP_START = 0
GROUP_END = 7

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

感谢贡献~

谢谢各位提供的事例代码。但是我在查看今日头条街拍的Ajax请求文件“Preview” 的时候其实并没有发现上述代码中的“cell_type”字段,而且‘data’字段下面根本没有像样的图片链接,甚至没有当前网页中的任何字段描述。所以我直接就运行上述代码。结果是确实会有内容被下载,但是查看文件名称会发现并不是当前网站中显示的内容,而且所有的标题文件夹下只有3张图片,通过搜索框搜索知道其实不应该只有3张图片。根据结果来看应该是被引导到其他的链接那里去了,而真正的链接被隐藏起来了。所以我想问这种反爬技术是一种什么技术,有什么方法可以应对呢?

i am just wondering that where i should write my the location of the pictures that i will download. i am just a fresh man, so i will appreciate it so much if you could give me a hand

为啥把响应里面的地址的list改为orign就是下载地址呀,求解释

为啥把响应里面的地址的list改为orign就是下载地址呀,求解释

改成origin下载原始图片,大图,不改的话下载下来的是小图

i just want to know that where i can put the data into my computer. In other words, can you make a note at the codes to let us understand it more clearly? all of us would appreciate it if you can take it into sincere consideration.

i just want to know that where i can put the data into my computer. In other words, can you make a note at the codes to let us understand it more clearly? all of us would appreciate it if you can take it into sincere consideration.

Hi buddy!
save_image is path of data.
'''img_path + os.path.sep + '{file_name}.{file_suffix}'''
just this.

谢谢各位提供的事例代码。但是我在查看今日头条街拍的Ajax请求文件“Preview” 的时候其实并没有发现上述代码中的“cell_type”字段,而且‘data’字段下面根本没有像样的图片链接,甚至没有当前网页中的任何字段描述。所以我直接就运行上述代码。结果是确实会有内容被下载,但是查看文件名称会发现并不是当前网站中显示的内容,而且所有的标题文件夹下只有3张图片,通过搜索框搜索知道其实不应该只有3张图片。根据结果来看应该是被引导到其他的链接那里去了,而真正的链接被隐藏起来了。所以我想问这种反爬技术是一种什么技术,有什么方法可以应对呢?

莫法用了,有反爬机制,可以获取到详细页面的链接,但是拿不到详细页面的图片链接,有js动态渲染,利用selenium也不行啊

爬完文件都是空的

今日头条加了一个滑动图片验证。

想问一下我看Preview的URL比楼主写的多几个参数,分别是app_name=web_search, en_qc=1, timestamp=1558628221572。 最后有个timestamps,这个我用time.time()调完比较了一下,整数部分url里多了2~3位......以上3个不用写也可以爬吗?

想问一下我看Preview的URL比楼主写的多几个参数,分别是app_name=web_search, en_qc=1, timestamp=1558628221572。 最后有个timestamps,这个我用time.time()调完比较了一下,整数部分url里多了2~3位......以上3个不用写也可以爬吗?

解决了没 老哥 我爬下来的只有空文件夹 小白刚学被这代码搞蒙了 QAQ

爬不了了

出错啦![Errno 22] 文件名、目录名或卷标语法不正确。: 'img\海报街拍| 会穿衣的男人有多迷人?看看这些男明星吧!'

怎么解决

能成功请求,但解析不了,如何解决?

一样@Hostage2018,GROUP_END<4运行不会出错,但GROUP_END>=4运行会出错

怎么解决

路径里不能有空格

@Anodsaber 什么意思

我想问一下,为什么必须在base_url里就加上关键字就好使,放在params里拼接起来就识别不到data呢?

莫法用了, 爬下来的data为None, 该加的参数都加了,已目前的水平找不出破解办法

莫法用了, 爬下来的data为None, 该加的参数都加了,已目前的水平找不出破解办法

可以爬,我把image_list里面的都爬下来了

莫法用了, 爬下来的data为None, 该加的参数都加了,已目前的水平找不出破解办法

去看我公开的issue,第一个就是

莫法用了, 爬下来的data为None, 该加的参数都加了,已目前的水平找不出破解办法

可以爬,我把image_list里面的都爬下来了

确实可以用,原来需要把关键字直接拼接到地址里,加到params中无效。 同问, WHY ?

莫法用了, 爬下来的data为None, 该加的参数都加了,已目前的水平找不出破解办法

可以爬,我把image_list里面的都爬下来了

确实可以用,原来需要把关键字直接拼接到地址里,加到params中无效。 同问, WHY ?

楼上@ Anodsaber代码更新的非常完整 , 可以参考下

为啥把响应里面的地址的list改为orign就是下载地址呀,求解释

改成origin下载原始图片,大图,不改的话下载下来的是小图
只是将list修改为origin,图片的url是错的哦!例如
小图的url:http://p3-tt.byteimg.com/list/300x196/pgc-image/ff30cdae588348c980d01c1b4234f1f3

大图的url:http://p3.pstatp.com/large/pgc-image/ff30cdae588348c980d01c1b4234f1f3
这个怎么解决呢?

为啥把响应里面的地址的list改为orign就是下载地址呀,求解释

改成origin下载原始图片,大图,不改的话下载下来的是小图
只是将list修改为origin,图片的url是错的哦!例如
小图的url:http://p3-tt.byteimg.com/list/300x196/pgc-image/ff30cdae588348c980d01c1b4234f1f3

大图的url:http://p3.pstatp.com/large/pgc-image/ff30cdae588348c980d01c1b4234f1f3
这个怎么解决呢?

你试一试把小图的url里的list改成origin的话的确是会显示一样的图片的虽然一个是http://p1.pstatp.com另外一个是http://p9-tt.byteimg.com但的确显示的是一样的。所以对爬取没有影响

为啥把响应里面的地址的list改为orign就是下载地址呀,求解释

改成origin下载原始图片,大图,不改的话下载下来的是小图
只是将list修改为origin,图片的url是错的哦!例如
小图的url:http://p3-tt.byteimg.com/list/300x196/pgc-image/ff30cdae588348c980d01c1b4234f1f3

大图的url:http://p3.pstatp.com/large/pgc-image/ff30cdae588348c980d01c1b4234f1f3
这个怎么解决呢?

你试一试把小图的url里的list改成origin的话的确是会显示一样的图片的虽然一个是http://p1.pstatp.com另外一个是http://p9-tt.byteimg.com但的确显示的是一样的。所以对爬取没有影响

返回的是这个欸?
{"error":"fail on get for 300x196/pgc-image/ff30cdae588348c980d01c1b4234f1f3, code=404 message=Not Found remoteAddr=10.24.31.68:8789 reqID=aae79724897dbedd"}
不知道怎么回事

想问一下我看Preview的URL比楼主写的多几个参数,分别是app_name=web_search, en_qc=1, timestamp=1558628221572。 最后有个timestamps,这个我用time.time()调完比较了一下,整数部分url里多了2~3位......以上3个不用写也可以爬吗?

timestamp=1558628221572 这个是毫秒级的,time.time()还要乘以1000再取整,不过我获取到的json数据里data是NONE

爬不了了样

就因为这个教程 导致头条的网站一直被扒 反扒技术越来越成熟 哈哈

现在新加了一个滑动验证 所有的代码又不能用了 写完什么都爬不下来