python爬虫 - 爬取html中的script数据(股票行情信息 - 雪球网 )

server/2024/11/15 4:55:48/

文章目录

  • 1. 分析页面内容数据格式
  • 2. 使用re.findall方法,爬取股票行情(返回信息异常)
  • 3. 使用re.findall方法,爬取股票行情(正常)
  • 4. 使用re.search 方法,爬取股票行情(返回信息异常)
  • 5. 使用re.search 方法,爬取股票行情(正常)

1. 分析页面内容数据格式

  • 打开 https://xueqiu.com/hq/detail?order=desc&orderBy=percent&type=sha&market=CN&first_name=0&second_name=3

  • 按F12(或 在网页上右键 --> 检查(Inspect))

  • 找到网页上的Network(网络)部分

  • 鼠标点击网页页面,按 Ctrl + R 刷新网页页面,可以看到 NetWork(网络)部分会刷新出很多的网络信息

  • 在Network中,选中“放大镜(过滤)”,输入网页上关注的某些信息,如:某个股票的名字

  • 在Name 列,找到 detail 相关的条目,右侧自动显示网页的相关内容:Headers, Preview, Response … …

  • 分析Response内容,所需要关心的内容,位于整个html页面的下面 内容;

在这里插入图片描述

2. 使用re.findall方法,爬取股票行情(返回信息异常)

要点:从 之间的数据都是json数据。 json.loads会自动将false转为False, true转为True


import re
import requests
import json# URL路径
url = "https://xueqiu.com/hq/detail?order=desc&orderBy=percent&type=sha&market=CN&first_name=0&second_name=3"response = requests.get(url)
str1 = response.content.decode()
print(f"str1 = [{str1}]")# 查找,使用正在表达式->取数组的第一个
result = re.findall("<script id=\"initStore\">window.__INITIAL_STORE__ = (.*?)</script>", str1)
print(result)

运行结果:


str1 = [403 Forbidden. Your IP Address: 124.202.215.98 .]
[]

报403错误,是因为一定要加上header才能成功访问(证明你是个浏览器,而不是机器人之类的)。

3. 使用re.findall方法,爬取股票行情(正常)

  • 拷贝其 cURL 信息

    如下所示。选中所需的条目,右键 --> Copy --> Copy as cURL

在这里插入图片描述


curl 'https://xueqiu.com/hq/detail?order=desc&orderBy=percent&type=sha&market=CN&first_name=0&second_name=3' \-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \-H 'Accept-Language: zh-CN,zh;q=0.9' \-H 'Cache-Control: max-age=0' \-H 'Connection: keep-alive' \-H 'Cookie: cookiesu=241712922404752; device_id=6a73424ed3aae5c44aeb59b0ddfbc91b; smidV2=20240412194646e6b74b8abc2e9e66b752832ee3e0ee4800ab44152b05354c0; s=bo11shkdxf; remember=1; xq_a_token=7ef03deb28d3396dc9d555329881fd9986211657; xqat=7ef03deb28d3396dc9d555329881fd9986211657; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE4NzgxNDkwNzEsImlzcyI6InVjIiwiZXhwIjoxNzE1NjYzMDk3LCJjdG0iOjE3MTMxMDA5Mjc2MjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.XUMYCQrcozefxhq-MVz8kB39_b5LC-wfZIEk7wytUPoTufTNNsYGnlxmoaT09V1_jadkKemvEfeDbneSTs6OaEp_aTNjMTN12xSKmUxwqqfpqzjWgZrOsUAwW3ArHYNrbT0llkfZR0nAh36p54Zl2ln-auokNRuEiqkrF-Ivpd8FPxs_b5SVXhbIM1mRgdTGyjWwCiHE9TNa7AzG870_fwimq0HefT88pjEvZSJ2tGdYAgWTYK6rmY_4nrjais4IodjkcmXpP7sFM_-OYN5NanzonuMK9OhbbOBiWNusORBeXoRUSDAyUFdNwc7Vsn5iDOm08FzVp-QmUtRGo2ne-Q; xq_r_token=62e0ff828b86cfa501f1520ad6570a99838e72e5; xq_is_login=1; u=1878149071; Hm_lvt_1db88642e346389874251b5a1eded6e3=1712922406,1713336629; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1713748510; .thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7=tS0VaWi7obEkSury7ep6QSmK3ixl5usjG6jN/M/opO639mLrQLd0/M5xBK+0SJT5l937mMvmchxm5Vx+uE7pdQ%3D%3D' \-H 'Sec-Fetch-Dest: document' \-H 'Sec-Fetch-Mode: navigate' \-H 'Sec-Fetch-Site: same-origin' \-H 'Sec-Fetch-User: ?1' \-H 'Upgrade-Insecure-Requests: 1' \-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' \-H 'sec-ch-ua: "Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"' \-H 'sec-ch-ua-mobile: ?0' \-H 'sec-ch-ua-platform: "Linux"' \--compressed
  • 利用工具 Convert curl commands to code https://curlconverter.com/html" title=python>python/ 进行转换

在这里插入图片描述

转换后信息如下图所示,选择【Copy to clipboard】,并黏贴到Pycharm开发环境中即可直接使用:

转换后信息如下图所示,请关注: header 中的 传输格式为: text/html

在这里插入图片描述


import re
import requests
import jsoncookies = {'cookiesu': '241712922404752','device_id': '6a73424ed3aae5c44aeb59b0ddfbc91b','smidV2': '20240412194646e6b74b8abc2e9e66b752832ee3e0ee4800ab44152b05354c0','s': 'bo11shkdxf','remember': '1','xq_a_token': '7ef03deb28d3396dc9d555329881fd9986211657','xqat': '7ef03deb28d3396dc9d555329881fd9986211657','xq_id_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE4NzgxNDkwNzEsImlzcyI6InVjIiwiZXhwIjoxNzE1NjYzMDk3LCJjdG0iOjE3MTMxMDA5Mjc2MjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.XUMYCQrcozefxhq-MVz8kB39_b5LC-wfZIEk7wytUPoTufTNNsYGnlxmoaT09V1_jadkKemvEfeDbneSTs6OaEp_aTNjMTN12xSKmUxwqqfpqzjWgZrOsUAwW3ArHYNrbT0llkfZR0nAh36p54Zl2ln-auokNRuEiqkrF-Ivpd8FPxs_b5SVXhbIM1mRgdTGyjWwCiHE9TNa7AzG870_fwimq0HefT88pjEvZSJ2tGdYAgWTYK6rmY_4nrjais4IodjkcmXpP7sFM_-OYN5NanzonuMK9OhbbOBiWNusORBeXoRUSDAyUFdNwc7Vsn5iDOm08FzVp-QmUtRGo2ne-Q','xq_r_token': '62e0ff828b86cfa501f1520ad6570a99838e72e5','xq_is_login': '1','u': '1878149071','Hm_lvt_1db88642e346389874251b5a1eded6e3': '1712922406,1713336629','acw_tc': '2760827617137484501643918ec1ae269b915224c8ceb33aa4f000172e82d2','is_overseas': '0','Hm_lpvt_1db88642e346389874251b5a1eded6e3': '1713748456','.thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7': 'QJJzyMG6NouPrj6PoJFuvPJC+4F7scrY2K8/CLAPFi42vzykhTkQXza2jBNUSCvXzZGckUg7p8vUHwJaA/U2xQ%3D%3D',
}headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7','Accept-Language': 'zh-CN,zh;q=0.9','Cache-Control': 'max-age=0','Connection': 'keep-alive',# 'Cookie': 'cookiesu=241712922404752; device_id=6a73424ed3aae5c44aeb59b0ddfbc91b; smidV2=20240412194646e6b74b8abc2e9e66b752832ee3e0ee4800ab44152b05354c0; s=bo11shkdxf; remember=1; xq_a_token=7ef03deb28d3396dc9d555329881fd9986211657; xqat=7ef03deb28d3396dc9d555329881fd9986211657; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE4NzgxNDkwNzEsImlzcyI6InVjIiwiZXhwIjoxNzE1NjYzMDk3LCJjdG0iOjE3MTMxMDA5Mjc2MjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.XUMYCQrcozefxhq-MVz8kB39_b5LC-wfZIEk7wytUPoTufTNNsYGnlxmoaT09V1_jadkKemvEfeDbneSTs6OaEp_aTNjMTN12xSKmUxwqqfpqzjWgZrOsUAwW3ArHYNrbT0llkfZR0nAh36p54Zl2ln-auokNRuEiqkrF-Ivpd8FPxs_b5SVXhbIM1mRgdTGyjWwCiHE9TNa7AzG870_fwimq0HefT88pjEvZSJ2tGdYAgWTYK6rmY_4nrjais4IodjkcmXpP7sFM_-OYN5NanzonuMK9OhbbOBiWNusORBeXoRUSDAyUFdNwc7Vsn5iDOm08FzVp-QmUtRGo2ne-Q; xq_r_token=62e0ff828b86cfa501f1520ad6570a99838e72e5; xq_is_login=1; u=1878149071; Hm_lvt_1db88642e346389874251b5a1eded6e3=1712922406,1713336629; acw_tc=2760827617137484501643918ec1ae269b915224c8ceb33aa4f000172e82d2; is_overseas=0; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1713748456; .thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7=QJJzyMG6NouPrj6PoJFuvPJC+4F7scrY2K8/CLAPFi42vzykhTkQXza2jBNUSCvXzZGckUg7p8vUHwJaA/U2xQ%3D%3D','Sec-Fetch-Dest': 'document','Sec-Fetch-Mode': 'navigate','Sec-Fetch-Site': 'same-origin','Sec-Fetch-User': '?1','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36','sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Linux"',
}params = {'order': 'desc','orderBy': 'percent','type': 'sha','market': 'CN','first_name': '0','second_name': '3',
}response = requests.get('https://xueqiu.com/hq/detail', params=params, cookies=cookies, headers=headers)
str1 = response.content.decode()result = re.findall("<script id=\"initStore\">window.__INITIAL_STORE__ = (.*?)</script>", str1)
print(f"result = [{result}]")json_result = result[0]
json.loads(json_result)
print(f"json_result = [{json_result}]")# 解析 json 结构数据体
print(f'json_result.isMobile        = {json_result["isMobile"]}')
print(f'json_result.originalUrl     = {json_result["originalUrl"]}')
for item in json_result["initStore"]["tableData"]:print(f'symbol = {item["symbol"]}, 'f'name = {item["name"]}, 'f'涨跌幅 = {item["percent"]}%, 'f'当前价 = {item["current"]}, 'f'涨跌额 = {item["chg"]}, ')

运行结果:
在这里插入图片描述

4. 使用re.search 方法,爬取股票行情(返回信息异常)

要点:从 之间的数据都是json数据。 json.loads会自动将false转为False, true转为True


import requests
import re
import jsoncookies = {'cookiesu': '241712922404752','device_id': '6a73424ed3aae5c44aeb59b0ddfbc91b','smidV2': '20240412194646e6b74b8abc2e9e66b752832ee3e0ee4800ab44152b05354c0','s': 'bo11shkdxf','remember': '1','xq_a_token': '7ef03deb28d3396dc9d555329881fd9986211657','xqat': '7ef03deb28d3396dc9d555329881fd9986211657','xq_id_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE4NzgxNDkwNzEsImlzcyI6InVjIiwiZXhwIjoxNzE1NjYzMDk3LCJjdG0iOjE3MTMxMDA5Mjc2MjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.XUMYCQrcozefxhq-MVz8kB39_b5LC-wfZIEk7wytUPoTufTNNsYGnlxmoaT09V1_jadkKemvEfeDbneSTs6OaEp_aTNjMTN12xSKmUxwqqfpqzjWgZrOsUAwW3ArHYNrbT0llkfZR0nAh36p54Zl2ln-auokNRuEiqkrF-Ivpd8FPxs_b5SVXhbIM1mRgdTGyjWwCiHE9TNa7AzG870_fwimq0HefT88pjEvZSJ2tGdYAgWTYK6rmY_4nrjais4IodjkcmXpP7sFM_-OYN5NanzonuMK9OhbbOBiWNusORBeXoRUSDAyUFdNwc7Vsn5iDOm08FzVp-QmUtRGo2ne-Q','xq_r_token': '62e0ff828b86cfa501f1520ad6570a99838e72e5','xq_is_login': '1','u': '1878149071','Hm_lvt_1db88642e346389874251b5a1eded6e3': '1712922406,1713336629','acw_tc': '2760827617137484501643918ec1ae269b915224c8ceb33aa4f000172e82d2','is_overseas': '0','Hm_lpvt_1db88642e346389874251b5a1eded6e3': '1713748456','.thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7': 'QJJzyMG6NouPrj6PoJFuvPJC+4F7scrY2K8/CLAPFi42vzykhTkQXza2jBNUSCvXzZGckUg7p8vUHwJaA/U2xQ%3D%3D',
}headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7','Accept-Language': 'zh-CN,zh;q=0.9','Cache-Control': 'max-age=0','Connection': 'keep-alive',# 'Cookie': 'cookiesu=241712922404752; device_id=6a73424ed3aae5c44aeb59b0ddfbc91b; smidV2=20240412194646e6b74b8abc2e9e66b752832ee3e0ee4800ab44152b05354c0; s=bo11shkdxf; remember=1; xq_a_token=7ef03deb28d3396dc9d555329881fd9986211657; xqat=7ef03deb28d3396dc9d555329881fd9986211657; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE4NzgxNDkwNzEsImlzcyI6InVjIiwiZXhwIjoxNzE1NjYzMDk3LCJjdG0iOjE3MTMxMDA5Mjc2MjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.XUMYCQrcozefxhq-MVz8kB39_b5LC-wfZIEk7wytUPoTufTNNsYGnlxmoaT09V1_jadkKemvEfeDbneSTs6OaEp_aTNjMTN12xSKmUxwqqfpqzjWgZrOsUAwW3ArHYNrbT0llkfZR0nAh36p54Zl2ln-auokNRuEiqkrF-Ivpd8FPxs_b5SVXhbIM1mRgdTGyjWwCiHE9TNa7AzG870_fwimq0HefT88pjEvZSJ2tGdYAgWTYK6rmY_4nrjais4IodjkcmXpP7sFM_-OYN5NanzonuMK9OhbbOBiWNusORBeXoRUSDAyUFdNwc7Vsn5iDOm08FzVp-QmUtRGo2ne-Q; xq_r_token=62e0ff828b86cfa501f1520ad6570a99838e72e5; xq_is_login=1; u=1878149071; Hm_lvt_1db88642e346389874251b5a1eded6e3=1712922406,1713336629; acw_tc=2760827617137484501643918ec1ae269b915224c8ceb33aa4f000172e82d2; is_overseas=0; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1713748456; .thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7=QJJzyMG6NouPrj6PoJFuvPJC+4F7scrY2K8/CLAPFi42vzykhTkQXza2jBNUSCvXzZGckUg7p8vUHwJaA/U2xQ%3D%3D','Sec-Fetch-Dest': 'document','Sec-Fetch-Mode': 'navigate','Sec-Fetch-Site': 'same-origin','Sec-Fetch-User': '?1','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36','sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Linux"',
}params = {'order': 'desc','orderBy': 'percent','type': 'sha','market': 'CN','first_name': '0','second_name': '3',
}response = requests.get('https://xueqiu.com/hq/detail', params=params, cookies=cookies, headers=headers)html_doc = response.text
data = re.search(r"<script id=\"initStore\">window.__INITIAL_STORE__ = (.*?)</script>", html_doc)json_data = json.loads(data.group(1))
print(f"json_data = {json_data}")# pretty print the data:
print(json.dumps(json_data, indent=4))for item in json_data["initStore"]["tableData"]:print(f'symbol = {item["symbol"]}, 'f'name = {item["name"]}, 'f'涨跌幅 = {item["percent"]}%, 'f'当前价 = {item["current"]}, 'f'涨跌额 = {item["chg"]}, ')

运行结果:

在这里插入图片描述

name 字段显示异常字符。 只需要增加: response.encoding = ‘utf-8’ 即可。

5. 使用re.search 方法,爬取股票行情(正常)

增加: response.encoding = ‘utf-8’ 后代码:


import requests
import re
import jsoncookies = {'cookiesu': '241712922404752','device_id': '6a73424ed3aae5c44aeb59b0ddfbc91b','smidV2': '20240412194646e6b74b8abc2e9e66b752832ee3e0ee4800ab44152b05354c0','s': 'bo11shkdxf','remember': '1','xq_a_token': '7ef03deb28d3396dc9d555329881fd9986211657','xqat': '7ef03deb28d3396dc9d555329881fd9986211657','xq_id_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE4NzgxNDkwNzEsImlzcyI6InVjIiwiZXhwIjoxNzE1NjYzMDk3LCJjdG0iOjE3MTMxMDA5Mjc2MjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.XUMYCQrcozefxhq-MVz8kB39_b5LC-wfZIEk7wytUPoTufTNNsYGnlxmoaT09V1_jadkKemvEfeDbneSTs6OaEp_aTNjMTN12xSKmUxwqqfpqzjWgZrOsUAwW3ArHYNrbT0llkfZR0nAh36p54Zl2ln-auokNRuEiqkrF-Ivpd8FPxs_b5SVXhbIM1mRgdTGyjWwCiHE9TNa7AzG870_fwimq0HefT88pjEvZSJ2tGdYAgWTYK6rmY_4nrjais4IodjkcmXpP7sFM_-OYN5NanzonuMK9OhbbOBiWNusORBeXoRUSDAyUFdNwc7Vsn5iDOm08FzVp-QmUtRGo2ne-Q','xq_r_token': '62e0ff828b86cfa501f1520ad6570a99838e72e5','xq_is_login': '1','u': '1878149071','Hm_lvt_1db88642e346389874251b5a1eded6e3': '1712922406,1713336629','acw_tc': '2760827617137484501643918ec1ae269b915224c8ceb33aa4f000172e82d2','is_overseas': '0','Hm_lpvt_1db88642e346389874251b5a1eded6e3': '1713748456','.thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7': 'QJJzyMG6NouPrj6PoJFuvPJC+4F7scrY2K8/CLAPFi42vzykhTkQXza2jBNUSCvXzZGckUg7p8vUHwJaA/U2xQ%3D%3D',
}headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7','Accept-Language': 'zh-CN,zh;q=0.9','Cache-Control': 'max-age=0','Connection': 'keep-alive',# 'Cookie': 'cookiesu=241712922404752; device_id=6a73424ed3aae5c44aeb59b0ddfbc91b; smidV2=20240412194646e6b74b8abc2e9e66b752832ee3e0ee4800ab44152b05354c0; s=bo11shkdxf; remember=1; xq_a_token=7ef03deb28d3396dc9d555329881fd9986211657; xqat=7ef03deb28d3396dc9d555329881fd9986211657; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE4NzgxNDkwNzEsImlzcyI6InVjIiwiZXhwIjoxNzE1NjYzMDk3LCJjdG0iOjE3MTMxMDA5Mjc2MjYsImNpZCI6ImQ5ZDBuNEFadXAifQ.XUMYCQrcozefxhq-MVz8kB39_b5LC-wfZIEk7wytUPoTufTNNsYGnlxmoaT09V1_jadkKemvEfeDbneSTs6OaEp_aTNjMTN12xSKmUxwqqfpqzjWgZrOsUAwW3ArHYNrbT0llkfZR0nAh36p54Zl2ln-auokNRuEiqkrF-Ivpd8FPxs_b5SVXhbIM1mRgdTGyjWwCiHE9TNa7AzG870_fwimq0HefT88pjEvZSJ2tGdYAgWTYK6rmY_4nrjais4IodjkcmXpP7sFM_-OYN5NanzonuMK9OhbbOBiWNusORBeXoRUSDAyUFdNwc7Vsn5iDOm08FzVp-QmUtRGo2ne-Q; xq_r_token=62e0ff828b86cfa501f1520ad6570a99838e72e5; xq_is_login=1; u=1878149071; Hm_lvt_1db88642e346389874251b5a1eded6e3=1712922406,1713336629; acw_tc=2760827617137484501643918ec1ae269b915224c8ceb33aa4f000172e82d2; is_overseas=0; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1713748456; .thumbcache_f24b8bbe5a5934237bbc0eda20c1b6e7=QJJzyMG6NouPrj6PoJFuvPJC+4F7scrY2K8/CLAPFi42vzykhTkQXza2jBNUSCvXzZGckUg7p8vUHwJaA/U2xQ%3D%3D','Sec-Fetch-Dest': 'document','Sec-Fetch-Mode': 'navigate','Sec-Fetch-Site': 'same-origin','Sec-Fetch-User': '?1','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36','sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Linux"',
}params = {'order': 'desc','orderBy': 'percent','type': 'sha','market': 'CN','first_name': '0','second_name': '3',
}response = requests.get('https://xueqiu.com/hq/detail', params=params, cookies=cookies, headers=headers)
response.encoding = 'utf-8'html_doc = response.text
data = re.search(r"<script id=\"initStore\">window.__INITIAL_STORE__ = (.*?)</script>", html_doc)json_data = json.loads(data.group(1))
print(f"json_data = {json_data}")# pretty print the data:
print(json.dumps(json_data, indent=4))for item in json_data["initStore"]["tableData"]:print(f'symbol = {item["symbol"]}, 'f'name = {item["name"]}, 'f'涨跌幅 = {item["percent"]}%, 'f'当前价 = {item["current"]}, 'f'涨跌额 = {item["chg"]}, ')

运行结果:

在这里插入图片描述


http://www.ppmy.cn/server/15146.html

相关文章

(C++) 内类生成智能指针shared_from_this介绍

文章目录 &#x1f601;介绍&#x1f914;类外操作&#x1f605;错误操作&#x1f602;正确操作 &#x1f914;类内操作&#x1f62e;std::enable_shared_from_this<>&#x1f62e;奇异递归模板 CRTP&#xff08;Curiously Recurring Template Pattern&#xff09;&#…

考研日常记录(upd 24.4.24)

由于实在太无聊了 &#xff0c; 所以记录以下考研备考日常 &#xff0c; 增加一点成就感 &#xff0c; 获得一点前进动力。 文章目录 2024.4.18 周四课程情况&#xff1a;时间规划&#xff1a; 2024.4.19 周五课程情况&#xff1a;时间规划&#xff1a; 2024.4.20 周六2024.4.2…

mybatis快速入门-注解版

mybatis 使用注解&#xff0c;简化 xml 配置&#xff0c;汲及到动态 sql 或是多表查询&#xff0c;还是使用 xml 映射文件配置编写。(企业工作中&#xff0c;几乎全是 xml 配置&#xff0c;xml 的 sql 使用注解方式少,而类引用注解方式)。 注解 Select()&#xff1a;查询Inse…

尾矿库安全监测:仪器埋设与维护的关键要求

尾矿库作为矿业生产的重要组成部分&#xff0c;其安全运营对于保障人员生命安全和环境保护具有至关重要的意义。为了确保尾矿库的安全运行&#xff0c;及时发现潜在的安全隐患&#xff0c;必须采取有效的安全监测措施。本文将重点探讨尾矿库安全监测仪器的埋设及维护要求。 一、…

Centos7 的 Open Stack T 版搭建流程 --- (二)配置 SQL 数据库

配置 SQL 数据库 文章目录 配置 SQL 数据库&#xff08;1&#xff09;安装 MariaDB、MariaDB服务器 和 Python 2 PyMySQLcontroller &#xff08;2&#xff09;配置数据库文件controller &#xff08;1&#xff09;安装 MariaDB、MariaDB服务器 和 Python 2 PyMySQL controlle…

FANUC机器人socket通讯硬件配置

一、添加机器人选配包 Fanuc机器人要进行socket通讯&#xff0c;需要有机器人通讯的选配包&#xff0c;1A05B-2600-R648 User Socket Msg&#xff0c;1A05B-2600-R632 KAREL&#xff0c;1A05B-2600-R566 KAREL Diagnostic&#xff0c;1A05B-2600-J971 KAREL Use Sprt FCTN。 二…

在IDEA中使用.env文件配置信息

在软件开发过程中&#xff0c;经常需要使用配置信息来指定应用程序的行为&#xff0c;例如数据库连接参数、API密钥等。而将这些敏感信息硬编码在代码中是不安全的&#xff0c;因此通常会将其存储在配置文件中。 .env文件是一种常见的配置文件格式&#xff0c;它可以存储键值对…

新恒盛110kV变电站智能辅助系统综合监控平台+道巡检机器人

江苏晋控装备新恒盛化工有限公司是晋能控股装备制造集团有限公司绝对控股的化工企业&#xff0c;公司位于江苏省新沂市。新恒盛公司40•60搬迁项目在江苏省新沂市经济开发区化工产业集聚区苏化片区建设&#xff0c;总投资为56.64亿元&#xff0c;该项目是晋能控股装备制造集团重…