学知识拯救世界 发表于 2020-9-30 12:52:28

爬虫问题

#导入包
import requests
from lxml import etree

session = requests.Session()
#登录地址
url = 'https://www.lixinger.com/api/account/sign-in/by-account'

#UA
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'
    }

data = {
    'accountName': "zhang",
    'password': "zhang218"
    }
response = session.post(url = url,headers = headers, data = data)
print(response.status_code)

value_url = 'https://www.lixinger.com/analytics/index/dashboard/value'

value_param = {
    'source': 'all',
    'series': 'all',
    'metric-type': 'mcw',
    'granularity': 'y10',
    'sort-name': 'pe_ttm.cv',
    'sort-order': 'asc'
    }
value_response = session.get(url = value_url,headers = headers,data = value_param).text
print(value_response)
tree = etree.HTML(value_response)
result = tree.xpath('//div[@class = "mt-5 v-wf-data-booth success"]//ul[@class = "list-unstyled mt-4"]/li/span/text()')

print(result)

value_response中无法获取element的数据?

疾风怪盗 发表于 2020-9-30 13:30:21

本帖最后由 疾风怪盗 于 2020-9-30 13:33 编辑

试试看这样获取数据:
# 导入包
import requests

session = requests.Session()
# 登录地址
url = 'https://www.lixinger.com/api/account/sign-in/by-account'
# UA
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'
}
data = {
    'accountName': "zhang",
    'password': "zhang218"
}
response = session.post(url=url, headers=headers, data=data)
print(response.content.decode())


value_url = 'https://www.lixinger.com/api/analyt/stock-collection/price-metrics/indices/latest'

value_param = {
    'source': "all",
    'series': "style",
    'stockFollowedType': "all",
    'metric-type': '["mcw",]',
    'granularities':'["y10"]',
    'metricNames': ["pe_ttm", "pb", "ps_ttm", "dyr", "cpc"]
}
value_response = session.post(url=value_url, headers=headers, data=value_param)
print(value_response)
print(value_response.content.decode())

疾风怪盗 发表于 2020-9-30 13:30:58

[{"stockId":1000000000028,"date":"2020-09-28T16:00:00.000Z","cpc":0.0013,"groupId":"590ad461e462ea9389115e95","name":"180成长","areaCode":"cn","stockType":"index","exchange":"sh","stockCode":"000028","tickerId":28,"source":"csi","series":"style","launchDate":"2009-01-08T16:00:00.000Z","followedNum":74,"currency":"CNY","stocksNum":60},{"stockId":1000000000029,"date":"2020-09-28T16:00:00.000Z","cpc":-0.0074,"groupId":"590ad461e462ea9389115e95","name":"180价值","areaCode":"cn","stockType":"index","exchange":"sh","stockCode":"000029","tickerId":29,"source":"csi","series":"style","launchDate":"2009-01-08T16:00:00.000Z","followedNum":340,"currency":"CNY","stocksNum":60},{"stockId":1000000000918,"date":"2020-09-28T16:00:00.000Z","cpc":0.0039,"groupId":"590ad461e462ea9389115e95","name":"300成长","areaCode":"cn","stockType":"index","exchange":"sh","stockCode":"000918","tickerId":918,"source":"csi","series":"style","launchDate":"2008-01-20T16:00:00.000Z","followedNum":202,"currency":"CNY","stocksNum":100},{"stockId":1000000000919,"date":"2020-09-28T16:00:00.000Z","cpc":-0.0062,"groupId":"590ad461e462ea9389115e95","name":"300价值","areaCode":"cn","stockType":"index","exchange":"sh","stockCode":"000919","tickerId":919,"source":"csi","series":"style","launchDate":"2008-01-20T16:00:00.000Z","followedNum":2249,"currency":"CNY","stocksNum":100},{"stockId":1000000399296,"date":"2020-09-28T16:00:00.000Z","cpc":0.013,"groupId":"590ad461e462ea9389115e95","name":"创成长","areaCode":"cn","stockType":"index","exchange":"sz","stockCode":"399296","tickerId":399296,"source":"cni","series":"style","launchDate":"2019-01-22T16:00:00.000Z","followedNum":66,"currency":"CNY","stocksNum":50},{"stockId":1000000399326,"date":"2020-09-28T16:00:00.000Z","cpc":0.0073,"groupId":"590ad461e462ea9389115e95","name":"成长40","areaCode":"cn","stockType":"index","exchange":"sz","stockCode":"399326","tickerId":399326,"source":"cni","series":"style","launchDate":"2006-01-23T16:00:00.000Z","followedNum":72,"currency":"CNY","stocksNum":40}]

Process finished with exit code 0
页: [1]
查看完整版本: 爬虫问题