有没有大声帮帮忙啊。爬虫报错修不来
import base64import binascii
import json
import random
import requests
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from string import ascii_letters, digits
_charset = ascii_letters + digits
def rand_char(num=16):
return ''.join(random.choice(_charset) for _ in range(num))
def aes_encrypt(msg, key, iv='0102030405060708'):
def padded(msg):
pad = 16 - len(msg) % 16
return msg + pad * chr(pad)
msg = padded(msg)
cryptor = AES.new(key, IV=iv, mode=AES.MODE_CBC)
text = cryptor.encrypt(msg)
text = base64.b64encode(text)
return text
def gen_params(d, i):
text = aes_encrypt(d, '0CoJUm6Qyw8W8jud')
text = aes_encrypt(text, i)
return text
def rsa_encrypt(msg):
cryptor = RSA.construct((0x00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7,0x10001L))
text = cryptor.encrypt(msg[::-1], '')[0]
text = binascii.b2a_hex(text)
return text
def encrypt(query):
query = json.dumps(query)
rand_i = rand_char(16)
params = gen_params(query, rand_i)
enc_sec_key = rsa_encrypt(rand_i)
data = {
'params': params,
'encSecKey': enc_sec_key
}
return data
if __name__ == '__main__':
music_id = '483671599'
url = 'http://music.163.com/weapi/v1/resource/comments/R_SO_4_{}?csrf_token='.format(music_id)
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'music.163.com',
'Origin': 'http://music.163.com',
'Referer': 'http://music.163.com/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
}
query = {
'rid': 'R_SO_4_{}'.format(music_id),
'offset': '0',
'total': 'true',# 第一页时为true,其他页为false
'limit': '20',
'csrf_token': ''
}
data = encrypt(query)
r = requests.post(url, data=data, headers=headers)
print(r.content)
for item in r.json()['comments']:
print(item['content'])
上面是代码,跑不起来,
cryptor = RSA.construct((0x00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7,0x10001L))
这一句有问题怎么解决啊。百度查不到。
报什么错?? python3.6此库安装方式,需要pip3 install pycryptodome。
如有site-packages中存在crypto、pycrypto,在pip之前,需要pip3 uninstall crypto、pip3 uninstall pycrypto,否则无法安装成功。
----------------------------------------------------------
这里顺带说一下pycrypto,这个库已经有很久没有人维护了,如果需要安装此库,需要先安装 VC++ build tools
然后将 ~\BuildTools\VC\Tools\MSVC\14.15.26726\include 目录下的 stdint.h 拷贝到 C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\ucrt 下。(Win10 需管理员权限)
接着将同目录下的 inttypes.h 中的 #include <stdint.h> (第十四行),改成 #include "stdint.h"。
然后使用 pip3 install pycrypto,就能直接安装了。
注:如果不是业务需要,请尽可能使用 pycryptodome。
1047861073 发表于 2022-4-5 00:52
python3.6此库安装方式,需要pip3 install pycryptodome。
如有site-packages中存在crypto、pycrypto, ...
用了pycryptodome,又出现了新的错误 1047861073 发表于 2022-4-5 00:42
报什么错??
File "E:\原C盘\V3\venv\004.py", line 31
cryptor = RSA.construct((0x00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7, 0x10001L))
^
SyntaxError: invalid hexadecimal literal
pycharm上面就是0x10001L这里是红色的。
页:
[1]