Skip to content

Commit

Permalink
perf: 增加user_agent参数
Browse files Browse the repository at this point in the history
  • Loading branch information
zsrl committed Aug 8, 2023
1 parent 420a373 commit 81c49a8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

**2、安装`jsdom`**

新版hexin-v.js使用了`jsdom`这个包,所以需要你的Node.js环境中有这个包
新版hexin-v.js使用了`jsdom`这个包,所以需要你的Node.js环境中有这个包,建议在你自己的项目下本地安装

```
npm install jsdom -g
npm install jsdom
```

测试jsdom是否已在当前Node.js环境中
Expand Down Expand Up @@ -150,6 +150,10 @@ pywencai.get(query='昨日涨幅', sort_order='asc', loop=True, log=True, reques

**【注意】** 1、该参数只有结果范围DataFrame时有效。2、配置该参数后,loop参数会失效,结果只会返回前100条。

#### user_agent

非必填,默认为`None`,可以自己传`user_agent`,不使用随机的生成的`user_agent`

### 返回值

当查询的是列表时,该方法返回一个`pandas``Dataframe`
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pywencai"
version = "0.11.0"
version = "0.11.1"
description = ""
authors = ["pluto <[email protected]>"]
readme = "README.md"
Expand Down
14 changes: 9 additions & 5 deletions pywencai/headers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import os
import execjs
from fake_useragent import UserAgent

ua = UserAgent()

def get_token():
'''获取token'''
Expand All @@ -11,9 +8,16 @@ def get_token():
context = execjs.compile(jscontent)
return context.call("v")

def headers(cookie=None):
def headers(cookie=None, user_agent=None):

if user_agent is None:
from fake_useragent import UserAgent
ua = UserAgent()
user_agent = ua.random


return {
'hexin-v': get_token(),
'User-Agent': ua.random,
'User-Agent': user_agent,
'cookie': cookie
}
6 changes: 4 additions & 2 deletions pywencai/wencai.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def get_robot_data(**kwargs):
log = kwargs.get('log', False)
query_type = kwargs.get('query_type', 'stock')
cookie = kwargs.get('cookie', None)
user_agent = kwargs.get('user_agent', None)
request_params = kwargs.get('request_params', {})
data = {
'perpage': 10,
Expand All @@ -58,7 +59,7 @@ def do():
method='POST',
url='http://www.iwencai.com/customized/chart/get-robot-data',
json=data,
headers=headers(cookie),
headers=headers(cookie, user_agent),
**request_params
)
params = convert(res)
Expand Down Expand Up @@ -89,6 +90,7 @@ def get_page(**kwargs):
sleep = kwargs.pop('sleep', 0)
log = kwargs.pop('log', False)
cookie = kwargs.pop('cookie', None)
user_agent = kwargs.get('user_agent', None)
find = kwargs.pop('find', None)
query_type = kwargs.get('query_type', 'stock')
request_params = kwargs.get('request_params', {})
Expand Down Expand Up @@ -126,7 +128,7 @@ def do():
method='POST',
url=target_url,
data=data,
headers=headers(cookie),
headers=headers(cookie, user_agent),
timeout=(5, 10),
**request_params
)
Expand Down

0 comments on commit 81c49a8

Please sign in to comment.