使用get函数无法获取相对应的标签

# -*- coding: utf-8 -*-
import requests
import pandas as pd
from bs4 import BeautifulSoup
import re
import json
#import cx_Oracle
from sqlalchemy import create_engine
import sys

url = 'http://www.sac.net.cn/hyfw/hydt/'
for i in range(1):
    if i ==0:
        url1='http://www.sac.net.cn/hyfw/hydt/index.html'
    else:
        url1=url+'index_'+str(i)+'.html'
    print url1
    user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    directory = {'User-Agent': user_agent}
    response = requests.get(url1, headers=directory)
    # 告诉他这段文本是以utf8方式编码 所以要以utf8方式解码
    response.encoding = 'utf-8'
    bs = BeautifulSoup(response.text, 'html.parser')
    title = bs.find('title').string[5:12]
    print title
    print url[0:21]
    table = bs.find_all('table')
    for a in table:
        b = a.find_all(&#
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是使用Python实现随机旋转数据集图片并修改对xml标签文件的例子代码: ```python import os import random import numpy as np from PIL import Image import xml.etree.ElementTree as ET # 设置待处理的图片和标签文件所在的目录 img_dir = 'path/to/image/directory' anno_dir = 'path/to/annotation/directory' # 定义旋转角度范围和旋转步长 angle_range = range(-90, 90, 10) rotate_step = 10 # 定义图片处理函数 def process_image(img_file): # 打开图片文件 img = Image.open(img_file) # 随机选择旋转角度 angle = random.choice(angle_range) # 旋转图片 rot_img = img.rotate(angle) # 保存旋转后的图片文件 img_file_name = os.path.basename(img_file) img_file_ext = os.path.splitext(img_file_name)[1] rot_img_file_name = os.path.splitext(img_file_name)[0] + '_' + str(angle) + img_file_ext rot_img_file_path = os.path.join(img_dir, rot_img_file_name) rot_img.save(rot_img_file_path) # 处理标签文件 anno_file_name = os.path.splitext(img_file_name)[0] + '.xml' anno_file_path = os.path.join(anno_dir, anno_file_name) # 打开标签文件,解析XML内容 tree = ET.parse(anno_file_path) root = tree.getroot() # 获取图片尺寸,计算图片中心点坐标 width, height = img.size cx, cy = width / 2, height / 2 # 遍历XML中的所有对象,并更新它们的位置和尺寸信息 for object_elem in root.findall('object'): # 获取对象的位置和尺寸信息 bbox_elem = object_elem.find('bndbox') xmin = int(bbox_elem.find('xmin').text) ymin = int(bbox_elem.find('ymin').text) xmax = int(bbox_elem.find('xmax').text) ymax = int(bbox_elem.find('ymax').text) bbox_width = xmax - xmin bbox_height = ymax - ymin # 计算对象中心点坐标 obj_cx, obj_cy = xmin + bbox_width / 2, ymin + bbox_height / 2 # 计算对象中心点坐标相对于图片中心点坐标的偏移量,并旋转该偏移量 dx, dy = obj_cx - cx, obj_cy - cy dx_rot = dx * np.cos(np.radians(angle)) - dy * np.sin(np.radians(angle)) dy_rot = dx * np.sin(np.radians(angle)) + dy * np.cos(np.radians(angle)) # 计算旋转后的对象中心点坐标 obj_cx_rot, obj_cy_rot = cx + dx_rot, cy + dy_rot # 计算旋转后的对象位置和尺寸信息 xmin_rot = int(obj_cx_rot - bbox_width / 2) ymin_rot = int(obj_cy_rot - bbox_height / 2) xmax_rot = int(obj_cx_rot + bbox_width / 2) ymax_rot = int(obj_cy_rot + bbox_height / 2) # 更新对象位置和尺寸信息 bbox_elem.find('xmin').text = str(xmin_rot) bbox_elem.find('ymin').text = str(ymin_rot) bbox_elem.find('xmax').text = str(xmax_rot) bbox_elem.find('ymax').text = str(ymax_rot) # 保存旋转后的标签文件 rot_anno_file_path = os.path.join(anno_dir, os.path.splitext(rot_img_file_name)[0] + '.xml') tree.write(rot_anno_file_path) # 遍历图片目录,处理每张图片和对标签文件 for img_file in os.listdir(img_dir): if img_file.endswith('.jpg'): # 判断是否为图片文件 img_file_path = os.path.join(img_dir, img_file) process_image(img_file_path) ``` 在上面的代码中,我们首先定义了待处理的图片和标签文件所在的目录路径,然后定义了旋转角度范围和旋转步长,并编写了一个处理图片和标签文件的函数`process_image()`。该函数首先打开图片文件,随机选择旋转角度,然后旋转图片并保存旋转后的图片文件。接着,它打开对标签文件,解析XML内容,计算旋转后的对象位置和尺寸信息并更新XML标签。最后,它保存旋转后的标签文件。在主函数中,我们遍历图片目录,处理每张图片和对标签文件。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值