re提取不到数据,求大神解答

weixin_46609022 weixin_46609022 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
提取到的关键网页代码:
<tr onclick="location.href='/city/sz.html';" style="cursor: pointer;">
<th>1</th>
<th>
<a href="/city/sz.html" title="深圳房价行情,房价概况走势,数据分析"> 深圳</a>
</th>
<th>74,929</th>
<th class="red">+18.96%</th>
<th class="red">+2.86%</th>
</tr>, <tr onclick="location.href='/city/bj.html';" style="cursor: pointer;">
<th>2</th>
<th>
<a href="/city/bj.html" title="北京房价行情,房价概况走势,数据分析"> 北京</a>
</th>
<th>62,567</th>
<th class="green">-2.09%</th>
<th class="green">-4.76%</th>
</tr>, <tr onclick="location.href='/city/sh.html';" style="cursor: pointer;">
......后边同类型

我的代码:
import requests
from tool import useragenttool
import bs4
import re
import openpyxl

def open_url(url):
"""解析网址,获取源码信息"""
res = requests.get(url, headers=useragenttool.get_headers())
return res

def find_data(res):
datas = []
soup = bs4.BeautifulSoup(res.text, "html.parser")
content = soup.find(class_="gb-dataListBox")
# print(content)
target = content.find_all("tr", style="cursor: pointer;")
# print(target)
target = iter(target)

for each in target:
# print(each.text)
if each.text.isnumeric():
datas.append([
re.search(r'(.+)', next(target).text).group(1),
re.search(r'\d.*', next(target).text).group(),
re.search(r'\d.*', next(target).text).group(),
re.search(r'\d.*', next(target).text).group()])
print(datas)

return datas


def main():
url = "https://www.creprice.cn/rank/cityforsale.html"
res = open_url(url)
datas = find_data(res)


if __name__ == '__main__':
main()

为什么 print(datas)出来的datas列表空的啊,我要爬城市,房价还有后边两个百分数,新手百思不得其解,求大神解答
...全文
评论 3
收藏 已收藏
分享
返回顶部
weixin_46609022
脚本语言 37,737 成员 34,208 内容