使用正则表达式爬取雪球网页中的市盈率-(代码中变量:shiyinglv)属性时无法得到正确值,始终得到空列表

各位大侠,本人在使用正则表达式爬取雪球网页中的市盈率-(代码中变量:shiyinglv)属性时无法得到正确值,始终得到空列表,怀疑是正则表达式中有中文导致(代码中倒数第三行)

另外两个变量name,price均可正确爬取(对应的正则表达式中未使用中文)

求各位大侠不吝赐教,应该对获得变量shiyinglv的代码如何做改进才能得到正确的数值?

需要爬取的网页如下:
xueqiu.com/S/SH601318
程序运行结果为:
[‘中国平安(SH:601318)’] [‘85.18’] []
其中第三个值为shiyinglv,但始终得到的为空

import requests
import re
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36’
}
def getHTMLText(url):

try:

    r = requests.get(url, timeout = 30,headers=headers)

    r.raise_for_status()

    r.encoding = r.apparent_encoding

    print(r.encoding)

    return r.content

except:

    return ""

def getStockInfo(url):

html = getHTMLText(url)

name = re.findall('<div class="stock-name">(.*?)</div>',html.decode('utf-8'))

price = re.findall('<div class="stock-current"><strong>¥(.*?)</strong>',html.decode('utf-8'))

shiyinglv = re.findall('市盈率(动): <span>(.*?)</span>',html.decode('utf-8'))

print(name,price,shiyinglv)

getStockInfo(‘xueqiu.com/S/SH601318')

Jason990420
最佳答案
# '市盈率(动): <span>(.*?)</span>'      NG
# '市盈率\(动\):<span>(.*?)</span>'    OK

Two places

  1. : is different as
  2. use \( and \) for ( and )
3年前 评论
arbitter2021 (楼主) 3年前
Jason990420 (作者) 3年前
arbitter2021 (楼主) 3年前
讨论数量: 1
Jason990420
# '市盈率(动): <span>(.*?)</span>'      NG
# '市盈率\(动\):<span>(.*?)</span>'    OK

Two places

  1. : is different as
  2. use \( and \) for ( and )
3年前 评论
arbitter2021 (楼主) 3年前
Jason990420 (作者) 3年前
arbitter2021 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!